Android SQLite SELECT syntax error apex -
i created parametric query works perfectly. when string passed apex, error
sqliteexception: near "'%'": thi query
string sql = "select titolo, icona, colore, tipo, identificativo, dato_campo table " + "where titolo '%" + parametro + "%' " + "or dato_campo '%" + parametro + "%' group identificativo"; if parametro string of type stringa', error.
how can fix?
problem string has single quote characters breaks sql query string.
simulating parametro == test01 - ok
"select titolo, icona, colore, tipo, identificativo, dato_campo table titolo '%test01%' or dato_campo '%test01%' group identificativo"; simulating parametro == stringa' nok
"select titolo, icona, colore, tipo, identificativo, dato_campo table titolo '%stringa'%' or dato_campo '%stringa'%' group identificativo"; as can see, string producing '%stringa'%' invalid sql query.
you should escape character ' during query like: %stringa''%'.
so, can add follows before creating query string:
parametro = parametro.replaceall("\'","''"); string sql = "select titolo, icona, colore, tipo, identificativo, dato_campo table " + "where titolo '%" + parametro + "%' " + "or dato_campo '%" + parametro + "%' group identificativo"; this support issue facing now... gabe sechan mentioned on other answer, raw queries should discouraged.
update
safe way run query is:
string paramentro = "stringa'"; cursor cursor = db.query("tablename", new string [] {"titolo", "icona", "colore", "tipo", "identificativo", "dato_campo"}, "titolo ? or dato_campo ?", new string[]{"%"+paramentro+"%", "%"+paramentro+"%"}, "identificativo", null, null, null);
Comments
Post a Comment