文件已加密或不是数据库(Exception net.sqlcipher.database.SQLiteException)

时间:2014-02-27 07:51:48

标签: android database sqlite java-native-interface sqlcipher

我试图使用sqlcipher lib从现有数据库加密我的数据库但是在访问旧数据库时(即打开数据库)会出现以下异常:

 02-27 13:12:21.231: E/AndroidRuntime(14687): FATAL EXCEPTION: main
 02-27 13:12:21.231: E/AndroidRuntime(14687): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.sqlcipher/example.SQLDemoActivity}: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.access$600(ActivityThread.java:149)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.os.Handler.dispatchMessage(Handler.java:99)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.os.Looper.loop(Looper.java:153)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.main(ActivityThread.java:4987)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at java.lang.reflect.Method.invokeNative(Native Method)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at java.lang.reflect.Method.invoke(Method.java:511)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at dalvik.system.NativeStart.main(Native Method)
 02-27 13:12:21.231: E/AndroidRuntime(14687): Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2102)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1968)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:944)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at example.SQLDemoActivity.onCreate(SQLDemoActivity.java:42)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.Activity.performCreate(Activity.java:5020)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
 02-27 13:12:21.231: E/AndroidRuntime(14687):   ... 11 more

因为我的旧数据库没有加密所以我应该在那种情况下设置密码

我尝试过在密码字符串中传递null,也使用&#34;&#34;但没有运气

https://github.com/sqlcipher/android-database-sqlcipher/issues/124报告了问题

2 个答案:

答案 0 :(得分:7)

  

因为我的旧数据库没有加密所以我应该在那种情况下设置密码

Android版SQLCipher可以使用""作为密码打开未加密的数据库。

  

我尝试过在密码字符串中传递null,并且还使用“”但没有运气

然后:

  • 您的代码中有错误,或
  • 数据库已使用其他密码加密,或
  • 数据库已损坏。

答案 1 :(得分:0)

  1. 检查您是否使用了正确的导入: import net.sqlcipher.database.SQLiteDatabase;

  2. 在应用程序启动时加载libs,如下所示:

  3. SQLiteDatabase.loadLibs(this); //this = activity (usually a SplashScreen or MainActivity)
    db = DatabaseHandler.getInstance(this); //if it's singleton (and i suggest making it as such)
    db.getReadableDatabase(ConstantsHandler.DATABASE_PASSWORD);
    db.close();
    
    1. 确保它尚未使用密码加密

    2. 检查文件权限:

    3.   

      &LT;使用的许可&GT;机器人:名称=&#34; android.permission.WRITE_EXTERNAL_STORAGE&#34; /&GT;

           

      &LT;使用许可权   机器人:名称=&#34; android.permission.READ_EXTERNAL_STORAGE&#34; /&GT;