teekib
teekib

Reputation: 2821

Android unable to escaping single quote character while SQLite database update

Hi in my database i have a string like "computer's" , i am fetching it from database and displaying listview, when i click on the list item it should store to one of my database table. I am doing it as below but it is getting crashed

public void updateFav(String key, int value) {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        C_FAV=C_FAV.replaceAll("'","''");
        values.put(C_FAV, value);
        db.update(tableName, values, C_KEY + "='" + key.trim() + "'", null);
        db.close();
    }

Below is my trace

12-12 17:55:49.291: E/AndroidRuntime(18184): android.database.sqlite.SQLiteException: near "s": 
syntax error (code 1): , while compiling: UPDATE fav SET favorite=? WHERE key='computer's'

Upvotes: 0

Views: 292

Answers (1)

laalto
laalto

Reputation: 152807

You're escaping the apostrophes in C_FAV which seems to be a column name but not in key.

Consider using ? placeholder and bind arguments instead, e.g.

db.update(tableName, values, C_KEY + "=?", new String[] { key.trim() });

Upvotes: 2

Related Questions