Ale1794 Ale1794 - 1 year ago 116
SQL Question

Android dynamic query sql using like

I would make a query using

, using the % and a dynamic string, but i can't do it correctly.
I have a:

public Cursor query(String s)
String whereClause=COL_ATTRIBUTI + " like '%?%'";
String[] whereArgs=new String[] {s};
return getWritableDatabase().query(TABELLA_RICETTE,null,whereClause,whereArgs,null,null,null);

COL_ATTRIBUTI is the name of a column and TABELLA_RICETTE the name of the table.
I would like to obtain all the thing that have
somewhere, so i use the %. but my app crash. What is the correct syntax?

Answer Source

I think you need to put the % wildcards in the argument if you are using the ? placeholders:

String whereClause = COL_ATTRIBUTI + " like '?'";
String[] whereArgs = new String[] {"%" + s + "%"};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download