Reputation: 1932
I have a database with 6 columns, 2 of them are text format. When the user enters a string I would like to search for the string in the 2 text columns and return the rows. This is the code I have come up with:
mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE},
KEY_BODY + " LIKE '"+search_text+"%' OR "
+ KEY_TITLE + " LIKE '"+search_text+"%'" ,
null, null, null,KEY_DATE_INT + " DESC");
I am reasonable sure that this translates to
Select KEY_ROWID, KEY_TITLE, KEY_DATE from DATABASE_TABLE where {(KEY_BODY LIKE 'search_text') OR (KEY_BODY LIKE 'search_text')}
The problem I am having is that this only searches the first word of the columns. For example , if the text in the columns is "This is a test" and search string is "test" the query would not return the row. It will return the row if the search text is "this".
What am I doing wrong and is there a better way to implement text search inside a SQLite database in Android.
Upvotes: 3
Views: 1822
Reputation: 4008
Please try
mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_DATE},
KEY_BODY + " LIKE '%"+search_text+"%' OR "
+ KEY_TITLE + " LIKE '%"+search_text+"%'" ,
null, null, null,KEY_DATE_INT + " DESC");
The idea is that if you want your sub-string to be searched then you need to wrap up in %substring%
then your sub-string will get matched from whole string.
Upvotes: 8