Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLiteDatabase query with LIKE

Tags:

android

I have 3 names, Allakhazam, Beatbox and Cunning in my NAMES Table.

public Cursor fetchNamesByConstraint(String filter) {      mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,             KEY_NAME }, KEY_NAME + " LIKE ?",             new String[] { filter }, null, null, null,             null);      return mCursor; } 

I call the function with "A" as the filter, but my cursor is returning a 0 count when it should at least return me a 1. Anyone can see what's wrong with the code?

like image 888
Maurice Avatar asked Jan 31 '12 08:01

Maurice


1 Answers

this statement will return all the records whose keyname equals string specified by string, if you use wild card, then you can get desired results. Like:

mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,             KEY_NAME }, KEY_NAME + " LIKE ?",             new String[] { filter+"%" }, null, null, null,             null); 

Will Lists all the records starting with word in filter.

mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,             KEY_NAME }, KEY_NAME + " LIKE ?",             new String[] {"%"+ filter+ "%" }, null, null, null,             null); 

Will Lists all the records containing word in filter.

like image 78
jeet Avatar answered Sep 23 '22 20:09

jeet