我正在尝试查询我的SQLite数据库并隔离发送到游标的数据集。每个记录都有一个颜色字段,我想查询我的数据并返回特定的颜色。
当我运行它时,我得到一个SQLiteException:无法识别的标记“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;
}
答案 0 :(得分:1)
呀。您构建查询的方式有问题。看看这个例子。
public static Cursor getItemWithColor(ContentResolver contentResolver, String color) {
Uri uri = My.Table.CONTENT_URI;
final String[] PROJECTION = {
My.Table._ID, My.Table.COLOR
};
final String SELECTION = My.Table.COLOR + "=?";
final String[] SELECTION_ARGS = {
color
};
Cursor cursor = contentResolver.query(uri, PROJECTION, SELECTION, SELECTION_ARGS, null);
return cursor;
}
答案 1 :(得分:0)
我刚遇到这个问题,发现它可以通过在引号中包含selection参数来解决。仅使用标准选择参数字段似乎不适用于包含数字和字母的值。
因此得到的查询将是:
db.query(DATABASE_TABLE, DATABASE_STRUCTURE,
KEY_COLOR + "=\"" + color+"\"", null, null, null, null, null);