簡體   English   中英

將Ormlite與SQLCipher Android結合

[英]Combine Ormlite with SQLCipher Android

我想將ORMLite與SLQCipher一起使用。 我在以下鏈接中遵循了ge0rg的說明: 如何在Android中將ORMLite與SQLCipher一起使用? 但是在第4步中,我不知道如何添加密碼,因為在這里的源代碼中: https : //github.com/d-tarasov/ormlite-android-sqlcipher/blob/master/src/main/java/ com / j256 / ormlite / sqlcipher / android / apptools / OrmLiteSqliteOpenHelper.java中有一些帶有配置文件的構造函數。 我真的不知道如何使用它們。 我的應用程序在SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:99)上引發了NullPointerException異常。 我在哪里可以輸入密碼,為什么會出現NullPointerException? 謝謝!

如果您查看父OrmLiteSqliteOpenHelper類構造函數的文檔:

public OrmLiteSqliteOpenHelper(android.content.Context context,
                               String databaseName,
                               android.database.sqlite.SQLiteDatabase.CursorFactory factory,
                               int databaseVersion,
                               int configFileId)
Same as the other constructor with the addition of a file-id of the table config-file. 
See OrmLiteConfigUtil for details.    

Parameters:
configFileId - file-id which probably should be a R.raw.ormlite_config.txt or some 
               static value. 

它指導您查看OrmLiteConfigUtil以獲取詳細信息 總結-在Ice Cream Sandwich(Google API 15)之前,Android中對注釋方法的調用非常昂貴,並且在配置10-15個DAO時使人們看到2-3秒的啟動時間。 因此,他們添加了一個實用程序類,該類可以轉換您的注釋並編寫配置文件。 然后可以將該文件加載到DaoManager中,而無需在運行時調用任何注釋。

因此,根據您的minSDKVersion,如果它是Ice Cream Sandwich(Google API 15)或更高版本,則可以繼續使用注釋。 查看源代碼 ,您只需為configFile傳遞null即可:

public class MyDatabaseHelper extends OrmLiteSqliteOpenHelper {
    public static final String DATABASE_NAME = "Example.db";
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_PASSWORD = "Password";

    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION, (InputStream)null, DATABASE_PASSWORD);
    }
}

注意:為了避免構造函數產生歧義,請確保將其強制轉換為null。

並且如果您的minSDKVersion小於Google API 15(例如Honeycomb), 則應為您的類創建一個配置文件。

OrmLiteConfigUtil.writeConfigFile("ormlite_config.txt"); 

然后,您將R.raw.ormlite_config.txt傳遞給上面的構造函數,而不是null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM