Alex Curran Alex Curran - 29 days ago 11
Android Question

Wildcard not working in SQLite

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!

Answer

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.

Comments