3 回答
TA贡献1830条经验 获得超3个赞
无法在索引 1 处绑定参数,因为索引超出范围。该语句有 0 个参数。(这意味着它没有在查询中找到“?”,对吗?
不完全是。
虽然有一个......,顺便说一句,“过滤器”是一个方法参数)
问题是问号在带引号的字符串内。这使它成为纯文本,而不是参数占位符。这种变化似乎是你要找的:
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贡献1868条经验 获得超4个赞
完全不相关,但如果使用字符串连接,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 + "%" }
);
实际上,您只用于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贡献1777条经验 获得超10个赞
我不知道你怎么看这个:
'%%?%%'
可以,但是您必须将其更改为串联'%' || ? || '%':
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}
);
添加回答
举报