Sqliteopenhelper没有为sqlcipher调用onCreate

时间:2014-03-17 16:41:40

标签: android sqliteopenhelper sqlcipher

我已阅读过SQLcipher guid,所有步骤均在指导下完成。从未调用SQliteOpenHelper中的onCreate()。已经尝试了很多方法,但都不起作用。任何帮助将不胜感激。

代码如下:

package com.example.sqlcipher;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import android.content.Context;

import com.example.sqlcipher.PasswordContract.Entries;
import com.example.sqlcipher.PasswordContract.Fields;
import com.example.sqlcipher.PasswordContract.GroupFields;
import com.example.sqlcipher.PasswordContract.Groups;


public class DatabaseHelper extends SQLiteOpenHelper {

public static final String TAG = "MyDatabaseHelper";
public static final String DATABASE_NAME = "fingerprint_key.db";
public static final int DATABASE_VERSION = 1;

public interface Tables {
    public static final String ENTRIES = "entries";
    public static final String FIELDS = "fields";
    public static final String GROUP_FIELDS = "group_fields";
    public static final String GROUPS = "groups";
}

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    LogUtil.dLog(TAG, "----------create DatabaseHelper--------------");

}

public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
    SQLiteDatabase.loadLibs(context);
    LogUtil.dLog(TAG, "----------create DatabaseHelper------1--------");
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    LogUtil.dLog(TAG, "-----------on Create--------------");
    createTables(sqLiteDatabase);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

}

/**
 * create Tables for database.
 */
public void createTables(SQLiteDatabase db) {

    LogUtil.dLog(TAG, "create the strings of sql !");
    final String entriesSql = "create table if not exists "
            + Tables.ENTRIES + "("
            + Entries._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Entries.TITLE + " TEXT,"
            + Entries.NOTES + " TEXT,"
            + Entries.GROUP_ID + " INTEGER"
            + ")";

    final  String fieldsSql = "create table if not exists "
            + Tables.FIELDS + "("
            + Fields._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Fields.TITLE + " TEXT,"
            + Fields.VALUE + " TEXT,"
            + Fields.ENTRY_ID + " INTEGER,"
            + Fields.GROUPFIELD_ID + " INTEGER,"
            + Fields.IS_HIDDEN + " INTEGER"
            + ")";

    final String groupFieldsSql = "create table if not exists "
            + Tables.GROUP_FIELDS + "("
            + GroupFields._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + GroupFields.TITLE + " TEXT,"
            + GroupFields.GROUP_ID + " INTEGER,"
            + GroupFields.IS_HIDDEN + " INTEGER"
            + ")";

    final String groupsSql = "create table if not exists "
            + Tables.GROUPS + "("
            + Groups._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Groups.TITLE + " TEXT,"
            + Groups.ICON + " TEXT"
            + ")";

    db.execSQL(entriesSql);
    LogUtil.dLog(TAG, "create table entries");
    db.execSQL(fieldsSql);
    LogUtil.dLog(TAG, "create table filed");
    db.execSQL(groupFieldsSql);
    db.execSQL(groupsSql);
    LogUtil.dLog(TAG, "create tables is over! ");
    initDatabase(db);
}

private void initDatabase(SQLiteDatabase db) {
    String insertIntoGroups = "INSERT INTO groups " +
            "(" + "title, icon" +
            ")" +
            "VALUES";

}


}

调用SQLiteOpenHelper:

的地方
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DatabaseHelper mHelper = new DatabaseHelper(this, 
            DatabaseHelper.DATABASE_NAME, null, DatabaseHelper.DATABASE_VERSION);
    mHelper.getWritableDatabase(FingerPrintProvider.PASSWORD);

    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

如果您运行适用于Android的SQLCipher test suite,我们会test同时执行onCreateonUpgrade。您应该验证给定数据库的PRAGMA user_version返回值,并将其与您向DatabaseHelper提供的内容进行比较。如果数据库不存在,user_version将返回0,当您提供1或某个大于0的数字时,它将调用onCreate。< / p>