3 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
無法在索引 1 處綁定參數(shù),因?yàn)樗饕龇秶T撜Z句有 0 個(gè)參數(shù)。(這意味著它沒有在查詢中找到“?”,對嗎?
不完全是。
雖然有一個(gè)......,順便說一句,“過濾器”是一個(gè)方法參數(shù))
問題是問號在帶引號的字符串內(nèi)。這使它成為純文本,而不是參數(shù)占位符。這種變化似乎是你要找的:
getReadableDatabase().rawQuery(
String.format("SELECT lis.%s from %s lis LEFT JOIN %s li ON lis.%s = li.%s WHERE li.%s is NULL AND lis.%s LIKE ?;",
ListItemSuggestion.COLUMN_NAME,
ListItemSuggestion.TABLE_NAME,
ListItem.TABLE_NAME,
ListItemSuggestion.COLUMN_NAME,
ListItem.COLUMN_NAME,
ListItem.COLUMN_NAME,
ListItemSuggestion.COLUMN_NAME
),
new String[]{ "%" + filter + "%" }
);

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
完全不相關(guān),但如果使用字符串連接,SQL 將更容易閱讀:
getReadableDatabase().rawQuery(
"SELECT lis." + ListItemSuggestion.COLUMN_NAME +
" FROM " + ListItemSuggestion.TABLE_NAME + " lis" +
" LEFT JOIN " + ListItem.TABLE_NAME + " li" +
" ON li." + ListItem.COLUMN_NAME + " = lis." + ListItemSuggestion.COLUMN_NAME +
" WHERE li." + ListItem.COLUMN_NAME + " IS NULL" +
" AND lis." + ListItemSuggestion.COLUMN_NAME + " LIKE ?",
new String[] { "%" + filter + "%" }
);
實(shí)際上,您只用于li存在性檢查,所以這樣寫:
getReadableDatabase().rawQuery(
"SELECT " + ListItemSuggestion.COLUMN_NAME +
" FROM " + ListItemSuggestion.TABLE_NAME +
" WHERE " + ListItemSuggestion.COLUMN_NAME + " LIKE ?" +
" AND " + ListItemSuggestion.COLUMN_NAME + " NOT IN (" +
" SELECT li." + ListItem.COLUMN_NAME +
" FROM " + ListItem.TABLE_NAME + " li )",
new String[] { "%" + filter + "%" }
);

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
我不知道你怎么看這個(gè):
'%%?%%'
可以,但是您必須將其更改為串聯(lián)'%' || ? || '%':
getReadableDatabase().rawQuery(
String.format("SELECT lis.%s from %s lis LEFT JOIN %s li ON lis.%s = li.%s WHERE li.%s is NULL AND lis.%s LIKE '%' || ? || '%';",
ListItemSuggestion.COLUMN_NAME,
ListItemSuggestion.TABLE_NAME,
ListItem.TABLE_NAME,
ListItemSuggestion.COLUMN_NAME,
ListItem.COLUMN_NAME,
ListItem.COLUMN_NAME,
ListItemSuggestion.COLUMN_NAME
),
new String[]{filter}
);
添加回答
舉報(bào)