使用绿色dao时,需要进行一些代码自定义,以便从一个模式更新到下一个模式。对于我之前的需求,使用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);
}
到目前为止它的效果很好。但是对于我当前的架构,我在表之间添加了更多连接,并且从旧版本更新后,我发现崩溃表明新列不存在。
在greendao中有添加新列的方法吗?我是否需要以旧学校的方式编写sqlite代码才能实现这一目标? (任何代码样本都很受欢迎)
提前致谢
答案 0 :(得分:10)
示例:
@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. */
}
}