我在我的应用中将Sqlcipher for Android从3.5.7升级到4.1.3。
对于使用Sqlcipher 3创建的现有数据库,我需要进行自定义迁移,因为它基于自定义参数,this article的选项3也对此进行了说明。
尝试打开数据库时遇到此异常。
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
这是我的代码:
SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
public void preKey(SQLiteDatabase database) {
database.rawExecSQL("PRAGMA kdf_iter=1000;");
database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
database.rawExecSQL("PRAGMA cipher_page_size = 4096;");
}
public void postKey(SQLiteDatabase database) {
database.rawExecSQL("PRAGMA cipher_compatibility=3;");
}
};
// this line generate the exception
SQLiteDatabase database = SQLiteDatabase.openDatabase(oldDatabaseFile.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE, mHook);
...
// migration code with ATTACH, sqlcipher_export etc...
...
该文件存在且密码正确:如果我降级Sqlcipher库,则同一段代码有效。我做错了吗?
我发现了错误:
我使用参数cipher_page_size = 4096
和sqlchipher 3创建了数据库,但是不接受使用默认值。现在尝试迁移以指定参数“已使用”,但该参数不被接受。我只需要删除此参数
private final SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
public void preKey(SQLiteDatabase database) {
}
public void postKey(SQLiteDatabase database) {
database.rawExecSQL("PRAGMA cipher_compatibility=3;");
database.rawExecSQL("PRAGMA kdf_iter=1000;");
database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
}
};