Reputation: 9908
I have an android application with a SQLite database and I am trying to implement a search interface. First of all, I realize that making a FTS database is the way to go, but I thought that I could use a LIKE statement and get similar results if I use wildcards. That way the interface can be tested and the backend updated later.
Here is how I build the query:
public Cursor getMatching(String query) {
Cursor mCursor = db.query(false, TABLE, new String[] { KEY_ROWID,
KEY_NAME }, KEY_NAME + " LIKE ?",
new String[] { "%" + query + "%" }, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
KEY_NAME is the text column that I want results for, and query is the lowercase string that I want to match against. I would expect that it would return results where the query appears anywhere in the name, case insensitive. However, what I observe is different. The results of querying "" (empty string):
Coch**** ****
Squi*** St*****
Owe** ****
Smi** Ca****
G. Bur** Jo******
Gr******* Brown
Now when I query "o":
Owe** ****
G. Bur** Jo******
Gr******* Brown
Oddly enough, the first result is filtered out, although it contains an 'o'. When I query "ow":
Gr******* Brown
And finally when I query with "own":
null
Here is the method I use to handle the cursor:
public static void logMatches(Context context, String query) {
DBAdapter adapter = new DBAdapter(context);
adapter.open();
Cursor c = adapter.getMatching(query);
if (c == null)
return;
while (c.moveToNext()) {
String name = c.getString(c
.getColumnIndex(DBAdapter.KEY_NAME));
Log.d(TAG, name);
}
c.close();
adapter.close();
}
Where DBAdapter contains the SQLiteOpenHelper. The context I am passing in comes from a ContentProvider by using the getContext() method.
I want to implement a FTS table eventually, but my database is quite small now so I hoped that I could implement similar functionality using existing tables for now. Is it possible to do what I want with the LIKE clause? Am I making an obvious mistake?
Upvotes: 2
Views: 2514