mardi 3 février 2015

Escaping a character in Android and SQLite, android.database.sqlite.SQLiteException

Basically I'm trying to execute a query that goes like this:



String query = "SELECT * FROM coffee_table "+
"WHERE " + CommonConstants.C_PRODUCT + " LIKE ? "+
"ORDER BY " + CommonConstants.C_PRODUCT + " ASC ";


Here's my Cursor



Cursor cursor = mDb.rawQuery(query, new String[] {"%" + searchTerm + "%"});


where searchTerm may have an apostrophe. For example, Arby's or O'Connor. You get the idea.


I've looked at many stackoverflow questions but none of them seem to help. I've looked at: SQLite (like) query returning all results when quried with special characters


How do I deal with quotes ' in SQL


Android quotes within an sql query string


I've also tried just replacing ' with '' like this



searchTerm = searchTerm.replaceAll("'", "''");


and running Cursor cursor = mDb.rawQuery(query, null);


I'm stuck. Here's the error I get:



android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: SELECT * FROM coffee_table WHERE product = 'Arby's Jamocha Shake'

Aucun commentaire:

Enregistrer un commentaire