【问题标题】:Combine Ormlite with SQLCipher Android结合 Ormlite 与 SQLCipher Android
【发布时间】:2014-07-13 05:56:03
【问题描述】:

我想将 ORMLite 与 SLQCipher 一起使用。我在此链接中遵循了 ge0rg 的说明: How can I use ORMLite with SQLCipher together in Android? 但是在第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? 谢谢!

【问题讨论】:

    标签: java android ormlite sqlcipher


    【解决方案1】:

    如果您查看父 OrmLiteSqliteOpenHelper class constructor 的文档:

    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 for details。总而言之,在 Ice Cream Sandwich (Google API 15) 之前,Android 中对注释方法的调用非常非常昂贵,这导致人们在配置 10-15 个 DAO 时看到 2-3 秒的启动时间。所以他们添加了一个实用程序类来转换你的注释并编写一个配置文件。然后可以将该文件加载到 DaoManager 中,而无需对注解进行任何运行时调用。

    因此,根据您的 minSDKVersion,如果它是 Ice Cream Sandwich (Google API 15) 或更高版本,那么您可以继续使用注释。 Looking at the source,您只需为 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。

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多