Skip to content Skip to sidebar Skip to footer

String Encoding Error In Sqlitedatabase.rawquery

I am building a SQLite query as a String with String.format and passing it to SQLiteDatabase.rawQuery. The integer parameter I'm passing at the end is hard coded to zero (I'm looki

Solution 1:

You could try,

String sql = "SELECT * FROM ? WHERE ? > ?";
String[] selectionArgs = newString[] { TableName, ColumnName, "0"};
Cursor c = db.rawQuery(sql, selectionArgs);

However, I strongly suggest you make direct use of the SQLiteDatabase's query method instead. Given that it protects against SQL injections, it seems likely it will also protect against the strange encoding bugs too.

String[] selectionArgs = newString[] { ColumnName };
Cursor c = db.query(TableName, null, "? > 0", selectionArgs, null);

Solution 2:

I agree that query rather than rawQuery is a better solution, but String.Format() has a form that selects the locale. Not using this has bitten me in a few similar circumstances:

String query = String.format(Locale.US,
                             "SELECT * FROM %s WHERE %s > %d",
                             TableName,
                             ColumnName,
                             0);

Post a Comment for "String Encoding Error In Sqlitedatabase.rawquery"