我的 Android 应用程序中有一个 SQLite 数据库。我希望能够根据数据库中的 YEAR 列选择一个随机行。我已经能够从整个表中选择一个随机行,如下所示:
public String[] getTEST3RandChVerScrip() {
Cursor cursor = this.db.query("provtable Order BY RANDOM() LIMIT 1",
new String[] { KEY_MAKE, KEY_MODEL, KEY_NOTE }, null,
null, null, null, null);
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast();) {
String colStrings[] = new String[3];
colStrings[0] = cursor.getString(0);
colStrings[1] = cursor.getString(1);
colStrings[2] = cursor.getString(2);
return colStrings;
}
}
return null;
}
但是当我尝试添加一个选择参数时,应用程序会出错。这是我添加选择参数的地方:
public String[] getTEST3RandChVerScrip() {
Cursor cursor = this.db.query("provtable Order BY RANDOM() LIMIT 1",
new String[] { KEY_MAKE, KEY_MODEL, KEY_NOTE }, KEY_YEAR = "1964",
null, null, null, null);
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast();) {
String colStrings[] = new String[3];
colStrings[0] = cursor.getString(0);
colStrings[1] = cursor.getString(1);
colStrings[2] = cursor.getString(2);
return colStrings;
}
}
return null;
}
中的语法不正确KEY_YEAR = "1964"
吗?如果是这样,知道它应该是什么吗?
根据 tyczj 的回答,我已将查询修改为:
Cursor cursor = this.db.query("provtable Order BY RANDOM() LIMIT 1",
new String[] { KEY_MAKE, KEY_MODEL, KEY_NOTE }, KEY_YEAR + "=?",
new String[] {"1964"}, null, null, null);
...而且我仍然收到一条错误消息:06-20 15:18:33.644: E/AndroidRuntime(4758): android.database.sqlite.SQLiteException: near "WHERE": syntax error: , while compiling: SELECT make, model, note FROM provtable Order BY RANDOM() LIMIT 1 WHERE year=?