0

我有一个加密的数据库,在这个数据库中我有一些表。现在我想在我的一个表中添加一个额外的列,但从未调用过onUpgrade()方法。

我像这样初始化我的数据库:

private void initDatabase(){
    if (MyApp.this.daoSession == null) {
        DatabaseUpgradeHelper helper = new DatabaseUpgradeHelper(this, "myapp-db-enc");
        Database db = helper.getWritableDatabase("XXXXXXX");
        DaoMaster daoMaster = new DaoMaster(db);
        MyApp.this.daoSession = daoMaster.newSession();
    }
}

我的DatabaseUpgradeHelper对象基于此示例:示例

我的实现在这里:

public class DatabaseUpgradeHelper extends DaoMaster.EncryptedOpenHelper {

public DatabaseUpgradeHelper(Context context, String name) {
    super(context, name);
}

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
    List<Migration> migrations = getMigrations();

    // Only run migrations past the old version
    for (Migration migration : migrations) {
        if (oldVersion < migration.getVersion()) {
            migration.runMigration(db);
        }
    }
}

private List<Migration> getMigrations() {
    List<Migration> migrations = new ArrayList<>();
    migrations.add(new MigrationV3());
    //migrations.add(new MigrationV3());

    // Sorting just to be safe, in case other people add migrations in the wrong order.
    Comparator<Migration> migrationComparator = new Comparator<Migration>() {
        @Override
        public int compare(Migration m1, Migration m2) {
            return m1.getVersion().compareTo(m2.getVersion());
        }
    };
    Collections.sort(migrations, migrationComparator);

    return migrations;
}

private static class MigrationV3 implements Migration {

    @Override
    public Integer getVersion() {
        return 3;
    }

    @Override
    public void runMigration(Database db) {
        // Add new column to user table
        db.execSQL("ALTER TABLE " + GlobalSettingsEntityDao.TABLENAME + " ADD COLUMN " + GlobalSettingsEntityDao.Properties.FeatureScreenLastSeenVersion.columnName + " INTEGER");
    }
}

private interface Migration {
    Integer getVersion();

    void runMigration(Database db);
}
}

我调试了我的代码,但应用程序从未调用onUpgrade(Database db, int oldVersion, int newVersion)方法。为什么?

更新:我使用 greenDao 3.1.1

UPDATE2:这是一个例外:

net.sqlcipher.database.SQLiteException: no such column: T.FEATURE_SCREEN_LAST_SEEN_VERSION: , while compiling: SELECT T."_id",T."PINLOCK_PROTECTION",T."PLACES_LAST_SYNC_TIME",T."LAST_SERVER_PREFIX",T."LANGUAGE",T."WELCOME_SCREEN_LAST_SEEN_VERSION",T."LAST_VERSION_CHECK",T."STORE_NETWORK_LOG_DAY",T."IS_FIRST_LOGIN",T."KEEP_LOGGED_IN",T."FEATURE_SCREEN_LAST_SEEN_VERSION" FROM "GLOBAL_SETTINGS_ENTITY" T 
                                                            at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
                                                            at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
                                                            at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
                                                            at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:83)
                                                            at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:49)
                                                            at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
                                                            at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1762)
                                                            at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1727)
                                                            at de.greenrobot.dao.database.EncryptedDatabase.rawQuery(EncryptedDatabase.java:31)
                                                            at de.greenrobot.dao.query.Query.list(Query.java:76)
                                                            at de.greenrobot.dao.query.QueryBuilder.list(QueryBuilder.java:389)
                                                            at myapp.app.database.datasources.SettingsDatabaseHelper.getStoreNetworkLogDay(SettingsDatabaseHelper.java:600)
                                                            at myapp.app.database.DatabaseHelper.deleteOldNetworkLog(DatabaseHelper.java:272)
                                                            at myapp.app.activities.GateActivity$1.run(GateActivity.java:137)
                                                            at java.lang.Thread.run(Thread.java:818)
4

0 回答 0