Android:带有通配符 (%) 的 SQL rawQuery
我有一个 rawQuery()
,其以下 sql 字符串与此类似:
selectionArgs = new String[] { searchString };
Cursor c = db.rawQuery("SELECT column FROM table WHERE column=?", selectionArgs);
但现在我必须在搜索中包含通配符,因此我的查询如下所示:
SELECT column FROM table WHERE column LIKE 'searchstring%'
但是当查询包含单引号时,会抛出以下 SQLite 异常: android.database.sqlite.SQLiteException: bind or column index out of range
如何在 a 中运行带有 selectArgs 的 rawQuery带有通配符元素的 SQL 查询?
I'm having a rawQuery()
with following sql string similar to this:
selectionArgs = new String[] { searchString };
Cursor c = db.rawQuery("SELECT column FROM table WHERE column=?", selectionArgs);
but now I have to include a wildcard in my search, so my query looks something like this:
SELECT column FROM table WHERE column LIKE 'searchstring%'
But when the query contains single quotes the following SQLite Exception is thrown: android.database.sqlite.SQLiteException: bind or column index out of range
How can I run a rawQuery with selectionArgs inside a SQL query with wildcard elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将
%
附加到selectionArgs本身:注意:相应地,
searchString
字符串中的%
和_
仍然用作通配符!You have to append the
%
to the selectionArgs itself:Note: Accordingly
%
and_
in thesearchString
string still work as wildcards!Sqlite 框架自动在 ? 两边加上单引号。性格内在。
就是这样。
The Sqlite framework automatically puts single-quotes around the ? character internally.
That's it.
Brad Hein 和 Mannaz 的解决方案对我不起作用,但它确实有效:
Brad Hein's and Mannaz's solution did not work for me, but this did: