vendredi 29 avril 2016

Escaping special characters in sqlite when using = in query in android

I have database with a column itemitem that looks like this

aaabbb

aaa%bbb

aaa_bbb

aaa'bbb

My query looks like this where itemitems is one of the above fields;

    itemitems = itemitems.replaceAll("'","''");
    itemitems = itemitems.replaceAll("%","\\\\%");  // use \\\\ to generate \%
    itemitems = itemitems.replaceAll("_","\\\\_");

    itemitems = "'" + itemitems + "'";
    String query = "select * from " + table_ITEMS + " where itemitem = " + itemitems + " ESCAPE '\\'";

I get the following exception:

SQLiteException: near "ESCAPE": syntax error (code 1): , while compiling: select * from items where itemitem = 'aaa\_bbb' ESCAPE '\'

Note that I had to add an additional \ in the last line above for it to show \_. Even this entry field has problems with \.

However if I use itemitem like instead of itemitem =, I don't get the exception. But, I don't get the exact match either. This works fine for finding aaa'bbb with the itemitem = query.

It seems that the = query doesn't accept the ESCAPE clause. How can I escape the % and _ special characters?

Aucun commentaire:

Enregistrer un commentaire