我正在尝试查询我的 SQLite 数据库并隔离发送到光标的数据集。每条记录都有一个颜色字段,我想查询我的数据并返回特定颜色。
当我运行它时,我得到一个 SQLiteException: Unrecognized token "red"
这是我到目前为止所拥有的:
public Cursor getGroupRecords( String color ) {
Cursor mCursor = db.query(DATABASE_TABLE, DATABASE_STRUCTURE,
KEY_COLOR + "=" + color, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
这是我的日志:
08-23 13:52:00.560: D/szipinf(14301): Initializing inflate state
08-23 13:52:00.560: D/szipinf(14301): Initializing zlib to inflate
08-23 13:52:00.970: I/Database(14301): sqlite returned: error code = 1, msg = no such column: red
08-23 13:52:00.980: D/AndroidRuntime(14301): Shutting down VM
08-23 13:52:00.980: W/dalvikvm(14301): threadid=1: thread exiting with uncaught exception (group=0x2aac0560)
08-23 13:52:00.990: E/AndroidRuntime(14301): FATAL EXCEPTION: main
08-23 13:52:00.990: E/AndroidRuntime(14301): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cca.pqr/com.cca.pqr.PQRActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cca.pqr/com.cca.pqr.PQRListActivity}: android.database.sqlite.SQLiteException: no such column: red: , while compiling: SELECT _id, Color, Class, Subclass, Item, Value, Other, Remarks FROM PQR WHERE Color=red
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.os.Looper.loop(Looper.java:123)
08-23 13:52:00.990: E/AndroidRuntime(14301): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-23 13:52:00.990: E/AndroidRuntime(14301): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 13:52:00.990: E/AndroidRuntime(14301): at java.lang.reflect.Method.invoke(Method.java:507)
08-23 13:52:00.990: E/AndroidRuntime(14301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-23 13:52:00.990: E/AndroidRuntime(14301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-23 13:52:00.990: E/AndroidRuntime(14301): at dalvik.system.NativeStart.main(Native Method)
08-23 13:52:00.990: E/AndroidRuntime(14301): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cca.pqr/com.cca.pqr.PQRListActivity}: android.database.sqlite.SQLiteException: no such column: red: , while compiling: SELECT _id, Color, Class, Subclass, Item, Value, Other, Remarks FROM PQR WHERE Color=red
任何建议或帮助将不胜感激。提前致谢!
更新 这是工作代码:
public Cursor getGroupRecords( String color ) {
final String SELECTION = KEY_COLOR + "=?";
final String[] SELECTION_ARGS = { color };
Cursor mCursor = db.query(DATABASE_TABLE, DATABASE_STRUCTURE,
SELECTION, SELECTION_ARGS, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}