I keep trying to use wildcards in a search in an android app, and keep running into errors.
I’m performing a search on my application using the string below:
Cursor c_name = b.query("namedrxns", new String[] { "_id", "name" },
"name LIKE %?%", new String[] { query }, null, null, null);
when I use name LIKE %?%
or name=%?%
I get a “near “%”: syntax error: , while compiling: SELECT _id, name FROM namedrxns WHERE name=%?%” error.
but with name LIKE '%?%'
or name='%?%'
I get instead “bind or column index out of range: handle 0x40cb70”
Could someone please tell me what I’m doing wrong?
Thanks!
Append the %
to the query
parameter.
I.E.:
Cursor c_name = b.query("namedrxns", new String[] { "_id", "name" },
"name LIKE ?", new String[] { "%"+query+"%" }, null, null, null);
Like Thomas Mueller already said, please note that %
and _
within the value still work as wildcards.
Answer:
The following should work (but I didn’t test it with SQLite):
"name LIKE '%' || ? || '%'"
Please note that “%” and “_” within the value still work as wildcards.
Tags: androidandroid, sql, sqlite