GreenDao 版本变更

GreenDao version change

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

@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 它没有用,代码正在 运行 代码上重新生成?有人知道如何解决吗?

编辑:
我看到与此问题相关的几个问题,但它们是旧线程并没有帮助我。

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

Implement your own 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);
        }
    }
}

Instantiate your class

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

有关更多详细信息,请查看我是如何做到的 here