SQLiteException:将sqlcipher版本从3升级到4时文件不是数据库

时间:2019-05-10 10:07:50

标签: android sqlcipher-android

很久以前,我已经将sqlcipher集成到我们的项目中。由于某些安全问题,我们必须升级sqlcipher的版本。我已经浏览了以下链接

  1. https://discuss.zetetic.net/t/migrating-from-sqlcipher-3-5-1-to-4-1-3-in-android/3652
  2. https://discuss.zetetic.net/t/upgrading-sqlcipher-for-android-from-3-to-4/3580

但是我不知道在哪里添加该代码。如何将他们的代码与我的代码集成。

这是我的Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.axis.leadsloans, PID: 8798
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.axis.leadsloans/com.axis.leadsloans.nnmnu}: net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2928)
Caused by: net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;

这是我的数据库相关代码。

public class CRMDB {
    private SQLiteHelper sqLiteHelper;
    public SQLiteDatabase sqLiteDatabase;
    private Context context;
    .
    .
    .

    public CRMDB(Context c) {
        context = c;
        String s = "";
        SQLiteDatabase.loadLibs(c);
        SharedPreferences prefs = c.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
        empid = prefs.getString("empid", "");
    }


    public CRMDB openToWrite() throws SQLiteFullException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME + "_" + empid + ".db", null, MYDATABASE_VERSION);
        //App get crash on this line
        sqLiteDatabase = sqLiteHelper.getWritableDatabase("password");
        return this;
    }


    public CRMDB openToRead() throws SQLiteFullException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME + "_" + empid + ".db", null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase("password");
        return this;
    }


    public long insertData(String tablename, ContentValues contentvalue) {
        return sqLiteDatabase.insert(tablename, null, contentvalue);
    }

    public Cursor getData(String query) {
        Cursor c = sqLiteDatabase.rawQuery(query, null);
        return c;
    }

    public long updateData(String tablename, ContentValues contentvalue, String where, String whereArgs[]) {
        return sqLiteDatabase.update(tablename, contentvalue, where, whereArgs);
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(tb_task_createtable);
            db.execSQL(tb_calls_createtable);
            db.execSQL(tb_messages_table);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}

请帮助我理解这一点。

1 个答案:

答案 0 :(得分:2)

your second link中的讨论显示了如何为此设置SQLiteDatabaseHook

    SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
        public void preKey(SQLiteDatabase database) {
        }

        public void postKey(SQLiteDatabase database) {
            database.rawExecSQL("PRAGMA cipher_compatibility=3;");
            database.rawExecSQL("PRAGMA kdf_iter=1000;");
            database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
            database.rawExecSQL("PRAGMA cipher_page_size = 4096;");
        }
    };

然后更改:

    public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);

    }

收件人:

    public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version, mHook);
    }

将钩子提供给SQLiteOpenHelper构造函数。