Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite Wildcards

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.

like image 298
erad Avatar asked Dec 03 '25 22:12

erad


1 Answers

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

... where TITLE LIKE ? or ISBN LIKE ?

Reference

like image 183
laalto Avatar answered Dec 06 '25 11:12

laalto