Android SQLite Wildcards

人走茶凉 提交于 2019-12-12 14:47:18

问题


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);

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.


回答1:


Replace = operator with LIKE to make % behave like wildcard:

... where TITLE LIKE ? or ISBN LIKE ?

Reference



来源:https://stackoverflow.com/questions/20878482/android-sqlite-wildcards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!