Android SQLite Wildcards

android, android-sqlite, sqlite

Solution

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

... where TITLE LIKE ? or ISBN LIKE ?

Reference

Problem

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.

Original source