GreenDAO cannot access existed database after upgr

2019-08-09 16:33发布

问题:

I'm using GreenDAO with SQLCipher. After I upgrade SQLCipher from 3.5.2 to 4.0.1, my app cannot access the old encrypted database. I have already searched for a solution and found out that I need to run PRAGMA cipher_migrate in postKey of the SQLiteDatabaseHook to migrate my database. I tried like this, but nothing changes.:

    SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
        @Override
        public void preKey(SQLiteDatabase database) {

        }

        @Override
        public void postKey(SQLiteDatabase database) {
            database.execSQL("PRAGMA key = '" + key + "';");
            database.execSQL("PRAGMA cipher_migrate;");
        }
    };
    Database db = new EncryptedDatabase(SQLiteDatabase.openOrCreateDatabase("DB.db", key, null, hook));
    return new DaoMaster(db).newSession();