2014-09-25 18 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

相关问题