绿道的onUpdate。我如何添加新列旧表? [英] Green Dao onUpdate. How can I add new columns to old tables?

查看:176
本文介绍了绿道的onUpdate。我如何添加新列旧表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用绿色DAO有需要更新从一个架构下一些code定制。对于我先前的需求就足以增加使用code任何新表像这样DaoMaster.java:

 如果(oldVersion == SCHEMA_VERSION_OLD_VERSION&放大器;&安培; NEWVERSION == SCHEMA_VERSION){
             Log.i(greenDAO,从版本升级模式+ oldVersion +到+ NEWVERSION +通过添加语言和放大器;清单表);
             布尔ifNotExists = TRUE;
             NewTableDao.createTable(DB,ifNotExists);
             NewTable2Dao.createTable(DB,ifNotExists);
        }

到目前为止它的伟大的工作。然而,对于我目前的架构我已经添加了表之间更多的连接,并从旧版本升级后,我得到的崩溃,指示新列不存在。

有没有greendao的方式来添加新列?我需要写的sqlite的code在老同学的方式得到这回事?
(任何code样品欢迎的LOT)

在此先感谢


解决方案

  1. 有一个看一下SQL code用于创建表(位于DAO类)。

  2. 提取的SQL相关的变化。

  3. 手动更新退出表。

例如:

  @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
    Log.i(TAG,更新模式到版本:+ Integer.toString(oldVersion)+ - >中+ Integer.toString(静态网页));
    开关(oldVersion){
        情况1:
            / * V1-> V2:在第2版中的所有更改过来* /
            db.execSQL(ALTER TABLE+ MyDao.TABLENAME +ADD COLUMN'NEW_COL_1整数);
            db.execSQL(DROP TABLE IF EXISTS'MY_OLD_ENTITY');
            / *突破被省略的目的。 * /
        案例2:
            / * V2-> V3:在3个版本中的所有更改过来* /
            MyNewDao.createTable(分贝,真正的);
            db.execSQL(ALTER TABLE+ MyDao.TABLENAME +ADD COLUMN'NEW_COL_2'TEXT;);
            / *突破被省略的目的。 * /
    }
}

When using green dao there is some code customisation needed for updating from one schema to the next. For my earlier needs it was sufficient to add any new tables using code like this in DaoMaster.java:

 if(oldVersion==SCHEMA_VERSION_OLD_VERSION&& newVersion==SCHEMA_VERSION){
             Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by adding language & checklist table");                 
             boolean ifNotExists = true;
             NewTableDao.createTable(db, ifNotExists);
             NewTable2Dao.createTable(db, ifNotExists);              
        }

And so far it's worked great. However for my current schema I have added more connections between the tables, and after updating from the old version, I get crashes indicating that the new columns don't exist.

Is there a way in greendao to add new columns? do I need to write the sqlite code in a old school fashion to get this going? (Any code samples are A LOT of welcome)

Thanks in advance

解决方案

  1. Have a look at the SQL code for creating your table (located in the DAO-class).
  2. Extract the relevant change in the SQL.
  3. Update your exiting table manually.

Example:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i(TAG, "Update Schema to version: "+Integer.toString(oldVersion)+"->"+Integer.toString(newVersion));
    switch (oldVersion) {
        case 1:
            /* v1->v2: all changes made in version 2 come here */
            db.execSQL("ALTER TABLE "+MyDao.TABLENAME+" ADD COLUMN 'NEW_COL_1' INTEGER;");
            db.execSQL("DROP TABLE IF EXISTS 'MY_OLD_ENTITY'");
            /* break was omitted by purpose. */
        case 2:
            /* v2->v3: all changes made in version 3 come here */
            MyNewDao.createTable(db, true);
            db.execSQL("ALTER TABLE "+MyDao.TABLENAME+" ADD COLUMN 'NEW_COL_2' TEXT;");
            /* break was omitted by purpose. */
    }
}

这篇关于绿道的onUpdate。我如何添加新列旧表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆