2015-10-14 28 views
1

我的Android應用程序與填充提供的數據庫工作,並位於asset文件夾。我最近不得不開始添加私人數據,所以我用sqlcipher加密它。所以開始之前:提供我的Android應用程序與sqlcipher加密的數據庫不起作用,無法打開它

  • 加密進行得很順利,因爲我可以很容易地解密它並讀回它。這是通過sqlcipher shell完成的。

  • 一切工作像未加密後

我在做什麼了,就先推出我的應用程序的一個數據庫中的魅力,創造我的手機上的空白數據庫,複製和過去的內容的提供的數據庫,然後,用戶可以在手機上擁有自己的數據庫,而無需每次重新創建數據庫(數據庫相當大)。事實上,如果用戶已經在手機上擁有數據庫以便進一步發佈,那麼他將不必以這種方式重新創建它。

但與sqlcipher,它不再工作。

重要:當使用sqlcipher與非加密後分貝(使用「」作爲參數像openDatabase相關方法),這是工作爲好。

但是,當我嘗試用加密後數據庫,並與密碼,我有什麼是錯誤

file is encrypted or is not a database: create locale table failed

出現這種情況我創建了一個空白的數據庫使用此之後:

//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專家,使用數據庫已經很困難,但你可以猜測它現在正在進入另一個層次。任何幫助將受到歡迎。提前致謝 !

回答

1

最後解決我的問題以下鏈接經過長時間的搜尋後:Sqlcipher __ CREATE TABLE android_metadata failed

的誤差爲我打開我的數據庫,它改變了你是否使用sqlciphersqlite的方式到來。

舊方式使用sqlite打開DB:

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); 
相關問題