I’m trying to query my SQLite database and isolate the dataset sent to the cursor. Each record has a color field and I would like to query my data and return for a specific color.
When I run it, I get a SQLiteException: Unrecognized token “red”
Here is what I have so far:
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;
}
Here is my 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
Any suggestions or help would be greatly appreciated. Thanks in advance!
UPDATE
Here is the working code:
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;
}
Yeah. Something is wrong with how you are building your query. Check out this example.