android-Sqlcipher - 无法向数据库插入新行

时间:2014-07-08 06:58:14

标签: android sqlite android-sqlite

这是我的代码:

    SQLiteDatabase.loadLibs(this);
    File databaseFile = getDatabasePath("db");
    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "Ab61", null);
    database.execSQL("drop table comments");
    database.execSQL("CREATE TABLE IF NOT EXISTS comments(_id INTEGER PRIMARY KEY,spot_id INTEGER,server_id INTEGER" +
            ",description TEXT,uid TEXT ,dates TEXT,name TEXT)");

    database.execSQL("insert into comments values (null, 34, 3, 'asdasd', 'asdsad', 'asda', 'asds');");
    Cursor ss=db.rawQuery("SELECT * from comments", null);
    Log.v("this",Integer.toString(ss.getCount()));

我是db数据库,它是一个sqlite数据库。这是一个测试代码所以,我删除整个数据库,再次创建它并插入一个新行。

它没有任何错误。

游标getCount返回0,表示没有任何内容添加到数据库中。 它让我抓狂:(

你可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您正在将数据插入到与您查询的数据库database)不同的数据库(db)中。

答案 1 :(得分:0)

private void InitializeSQLCipher(){
        try {
            SQLiteDatabase.loadLibs(getApplicationContext());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        File databaseFile = getDatabasePath("demo.db");
        databaseFile.mkdirs();
        databaseFile.delete();
        database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null);
        database.execSQL("create table t1(a, b)");
        database.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"one for the money","two for the show"});
}
Cursor c = null;
c = database.rawQuery("SELECT a,b from t1", null);
if (c != null){
    if(c.moveToFirst()){
        while(!c.isAfterLast()){
            //Log.e("xlcx", c.getString(c.getColumnIndex("a")));
            Log.d("xlcx", c.getString(c.getColumnIndex("b")));
            c.moveToNext();
        }
    }
}

请参考

http://androidjug.blogspot.in/2014/07/sqlcipher-for-android-application.html