Search code examples
androidsqlitesql-like

android sqlite 'like' query and modulo expression


when i use LIKE query in android sql, i want 'contains' query about some input.

db.query(TABLE,
         new String[]{COLUMN},
         CONDITIONS + " LIKE " + "?",
         new String[]{("%" + input + "%")},
         null
         null
         ORDER_COLUMN + " DESC",
         null);

query is working well, but when i use '%' character in input, it working like empty string.

i want use '%' character which like normal string.

how can i do..?

thanks.


Solution

  • Use the LIKE y ESCAPE z form to specify an escape character for the LIKE expression, and then use that escape character to prefix any % in your LIKE expression that needs to be taken literally. For example:

    sqlite> create table t(a);
    sqlite> insert into t values('a%b');
    sqlite> insert into t values('ab');
    sqlite> insert into t values('aa%bb');
    sqlite> select * from t where a like '%a\%b%' escape '\';
    a%b
    aa%bb