Android SQLite通配符 [英] Android SQLite Wildcards
问题描述
我正在尝试使用通配符元素进行查询,以在我的SQLite表中搜索在特定变量中任意位置具有元素的条目.
I'm trying to query with a wildcard element to search my SQLite table for entries with an element at any position in a specific variable.
public String[] getCheckoutEntry(String title, String ISBN)
{
//Wild card Syntax
String titleWildCard = "%" + title + "%";
String ISBNWildCard = "%" + ISBN + "%";
//Query
String query = "select USER,AUTHOR,DATE,CALL_NUMBER,COUNT from CHECKOUT where TITLE = ? or ISBN = ? ";
String[] selectionArgs = new String[] {titleWildCard, ISBNWildCard};
Cursor cursor = db.rawQuery(query, selectionArgs);
我已经检查了6个有关Stack Overflow的答案,每个人都建议在变量上附加一个%",如上所示.当我尝试上述代码时,我的程序只是将包含%的titleWildCard解释为搜索查询,并尝试在表中查找%.
I've checked half a dozen answers on Stack Overflow already and everyone suggests simply appending a "%" to my variable, as seen above. When I try the above code, my program simply interprets titleWildCard including the % as the search query and tries to find % in the table.
如果我取出%",则该程序可以正常运行,只是只搜索确切的术语.
If I take out the "%" then the program runs without issue except that it only searches for the exact term.
推荐答案
将=
运算符替换为LIKE
,以使%
的行为类似于通配符:
Replace =
operator with LIKE
to make %
behave like wildcard:
... where TITLE LIKE ? or ISBN LIKE ?
这篇关于Android SQLite通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!