Sqlite android query to match column containing text

Trying to make a query that returns a match if this column contains text inside. For example, if I passed the word “really,” a column containing the text “This is a really long line of text” would match. So far, my queries are returned only if they match, so it will match the column containing "really", and that’s all.

I tried a couple of ways, but both seem to return the same result:

The first way (s is the string I'm passing):

Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_TITLE}, KEY_TITLE + " LIKE '%" + s + "%' COLLATE NOCASE", null, null, null, null); 

Next way

 String p_query = "SELECT COUNT(*) FROM data WHERE number=? COLLATE NOCASE"; 
+8
java android sqlite
source share
1 answer

Trying to make a query that returns a match if this column contains text inside.

This works for me, and it is parameterized:

 Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_TITLE}, KEY_TITLE + " LIKE ?", new String[] {"%" + s + "%"}, null, null, null); 

“Really” is a fairly clear-cut word, but if you are looking for something like “ice” and do not want “cubes”, “vices”, “ice cream”, etc., you will have to use different patterns, for example: "% " + s + " %" and s + " %" .

Or use an FTS table with a tokenizer , e.g. Porter Stemming.

+26
source share

All Articles