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;
}
これが私のlogcatです:
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;
}