我的 android 应用程序与提供的数据库一起工作,该数据库已填充并位于 asset
文件夹中。我最近不得不开始在其中添加私有(private)数据,所以我使用 sqlcipher
对其进行加密。所以在开始之前:
加密很顺利,因为我可以轻松解密并读回。这是通过
sqlcipher shell
完成的。在数据库未加密的情况下,一切都像魅力一样工作
我所做的是,在第一次启动我的应用程序时,在我的手机上创建一个空白数据库,复制并粘贴其中提供的数据库的内容,然后用户就可以在他的手机上拥有自己的数据库电话而不必每次都重新创建它(数据库非常大)。事实上,如果用户已经在他的手机上拥有用于进一步启动的数据库,他将不必以这种方式重新创建它。
但是对于 sqlcipher
,它不再起作用了。
重要提示:当将 sqlcipher 与非加密数据库一起使用时(使用“”作为相关方法的参数,如 openDatabase
),它也能正常工作。
但是当我尝试使用加密的数据库和密码时,我得到的是错误
文件已加密或不是数据库:创建语言环境表失败
这发生在我使用这个创建一个空白数据库之后:
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase("password").close();
然后我尝试按照以下指令打开它:
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, "password", null, SQLiteDatabase.OPEN_READWRITE);
然后发生错误。有人有想法吗?遗憾的是,我不是 Android 专家,使用数据库已经很困难,但你可以猜到它现在更上一层楼了。欢迎任何帮助。提前致谢!
最佳答案
通过以下链接进行长时间搜索后终于解决了我的问题:Sqlcipher __ CREATE TABLE android_metadata failed
错误来自于我打开数据库的方式,这会改变您是使用 sqlcipher
还是 sqlite
。
使用 sqlite
打开数据库的旧方法:
myDataBase = SQLiteDatabase.openDatabase(myPath, "password", null, SQLiteDatabase.OPEN_READWRITE);
使用 sqlcipher 的新方法,带有 SQLiteDatabaseHook
:
SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
public void preKey(SQLiteDatabase database) {
}
public void postKey(SQLiteDatabase database) {
database.rawExecSQL("PRAGMA cipher_migrate;");
}
};
myDataBase = SQLiteDatabase.openOrCreateDatabase(myPath, "password", null, hook);
关于android - 为我的 android 应用程序提供一个用 sqlcipher 加密的数据库不起作用,无法打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33119070/