0

我在我的 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在上面的代码中评论它不起作用,代码在运行代码时重新生成?有人知道如何解决吗?

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

4

1 回答 1

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);

有关更多详细信息,请在此处查看我是如何做到的。

于 2016-11-21T03:50:56.703 回答