GreenDao版本更改

时间:2016-11-19 09:30:03

标签: android greendao greendao-generator

我在我的android项目中使用GreenDAO。我在GreenDAO中看到了一个问题。每当我对现有表进行更改并更改版本以迁移更改时,GreenDAO都会删除所有表。 我试图在GreenDAOgenerated文件中进行更改,但它不是正确的程序,但它不起作用。

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
  Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
  dropAllTables(db, true);
  onCreate(db);
}

我试图在上面的代码中评论dropAllTables它不起作用,代码在运行代码时重新生成?有人知道如何解决它吗?

修改
我看到几个与此问题相关的问题,但它们是OLD线程并没有帮助我。

1 个答案:

答案 0 :(得分:1)

您可以展开DaoMaster.DevOpenHelper并提供自己的onUpgrade实施。

  

实施您自己的DevOpenHelper

public class MyOpenHelper extends DaoMaster.DevOpenHelper {

    //..
    //override the constructor here by calling super
    //..

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch(oldVersion) {
        case 1:
            //upgrade logic from version 1 to 2
            /* break was omitted by purpose. */
        case 2:
            //upgrade logic from version 2 to 3
            /* break was omitted by purpose. */
        case 3:
            //upgrade logic from version 3 to 4
            break;
        default:
            throw new IllegalStateException(
                    "unknown oldVersion " + oldVersion);
        }
    }
}
  

实例化您的班级

DaoMaster.DevOpenHelper helper = new MyOpenHelper(this, databaseName, null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);

有关详情,请查看我是如何做到的here