2014-09-25 21 views
0

根據SQLCipher官方網站的介紹,在「PRAGMA KEY ='testkey';」之後設置cipher_page_size。所以我創造我自己的DatabaseHelper如下:重置SQLCipher cipher_page_size導致打開數據庫錯誤

public class DatabaseHelper extends SQLiteOpenHelper { 

private final static String NEWDBNAME="my.db" ; 
public DatabaseHelper(Context context){ 
    super(context, NEWDBNAME, null, 0 ,new SQLiteDatabaseHook(){ 

     @Override 
     public void postKey(SQLiteDatabase arg0) { 
      arg0.rawExecSQL("PRAGMA cipher_page_size = 4096"); 
     } 

     @Override 
     public void preKey(SQLiteDatabase arg0) { 
     }}); 
    } 
    public void onCreate(SQLiteDatabase db) {  } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } 
    public SQLiteDatabase getWritableDatabase() 
    { 
     return getWritableDatabase("encodestr"); 
    } 
} 

問題就像是我的Android應用程序將得到一個錯誤:該文件不是一個分貝或者encripted ...

當我的意見:// arg0.rawExecSQL(「PRAGMA cipher_page_size = 4096」); ,它會運行良好。

有人知道這個問題嗎?

謝謝。

+0

您的數據庫是否已經存在於設備/模擬器上? – 2014-09-25 18:21:51

+0

是的,它的確如此。其實應用程序運行良好,而無需更改頁面大小。我只是想優化速度。 – David 2014-09-25 21:34:07

回答

0

如果數據庫已經存在,除非最初創建的cipher_page_size設置爲4096,否則需要使用新設置導出加密的數據庫。這樣做將允許所有頁面被重寫爲新的塊大小。舉個例子:

$ ./sqlcipher foo.db 
sqlcipher> PRAGMA key = 'foo'; 
sqlcipher> create table t1(a,b); 
sqlcipher> insert into table t1(a,b) values('one for the money', 'two for the show'); 
sqlcipher> .q 

現在foo.db具有默認cipher_page_size的1024,讓我們導出數據庫用新的4096 cipher_page_size

$ ./sqlcipher foo.db 
sqlcipher> PRAGMA key = 'foo'; 
sqlcipher> ATTACH DATABASE 'bar.db' as bar KEY 'foo'; 
sqlcipher> PRAGMA bar.cipher_page_size = 4096; 
sqlcipher> SELECT sqlcipher_export('bar'); 
sqlcipher> DETACH database bar; 
sqlcipher> .q 

現在,我們將需要查詢數據庫時指定cipher_page_size

$ ./sqlcipher bar.db 
sqlcipher> PRAGMA key = 'foo'; 
sqlcipher> PRAGMA cipher_page_size = 4096; 
sqlcipher> SELECT count(*) from sqlite_master; 
count(*) 
---------- 
1 
+0

親愛的,尼克。非常感謝。我會嘗試。 – David 2014-09-27 20:02:28

相關問題