SQLite不适用于Android

时间:2014-04-06 23:32:32

标签: android sqlite

我有一个非常简单的例子(在onClickListener按钮内,仅供参考):

DatabaseHandler dbHandler = new DatabaseHandler(
                v.getContext(), 
                v.getContext().getResources().getString(R.string.DATABASE_NAME));

dbHandler.getWritableDatabase().execSQL("CREATE TABLE IF NOT EXISTS test (abc TEXT);");

dbHandler.getWritableDatabase().rawQuery("INSERT INTO test (abc) VALUES ('blah');", null); 

Cursor test = dbHandler.getReadableDatabase().rawQuery("SELECT * FROM test;", null);
Log.e("TEST", test.toString());
Log.e("TEST", String.valueOf(test.getCount()));

类别:

public class DatabaseHandler extends SQLiteOpenHelper {

private static int DATABASE_VERSION = 2;

public DatabaseHandler(Context context, String dbName) {
    super(context, dbName, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
}

}

输出(它是一个按钮,我点击了3次):

04-07 01:31:51.047: E/TEST(11816): android.database.sqlite.SQLiteCursor@42e97400
04-07 01:31:51.047: E/TEST(11816): 0
04-07 01:31:51.677: E/TEST(11816): android.database.sqlite.SQLiteCursor@42ed3980
04-07 01:31:51.677: E/TEST(11816): 0
04-07 01:31:52.428: E/TEST(11816): android.database.sqlite.SQLiteCursor@42e755d8
04-07 01:31:52.438: E/TEST(11816): 0

我只是觉得它没有错。几分钟前我开始工作了。我不知道为什么它突然停止了工作。有人看到我的错误吗?

1 个答案:

答案 0 :(得分:1)

似乎rawQuery不适用于INSERT - 语句。使用execSQL有效。