简体   繁体   中英

Error android.database.sqlite.SQLiteException: no such column: while updating SQLite database

I have one column in SQLite database.It stores Text value.When new value is inserted I need to delete the previous value and add new value in Database. This is method in Class which extends SQLiteOpenHelper to update the column.

public void updateOffline(String data){
        SQLiteDatabase db=this.getWritableDatabase();
        String updateQuery="UPDATE "+OFFLINE_MODE +" SET "+OFFLINE_DATA+" = "+ data;
        db.execSQL(readQuery);
    }  

where data is the String I need to UPDATE
I am calling this method in one of Fragment as

db.updateOffline(tempArray.toString());  

I am getting error as

android.database.sqlite.SQLiteException: no such column: {"name":"jjajaja","phone":"4545454545","no_people":"2","booktime":"12:46 pm"} (code 1): , while compiling: UPDATE offlinedata SET  offlinequeue =[{"name":"jjajaja","phone":"4545454545","no_people":"2","booktime":"12:46 pm"}]  

How to resolve this ?

You cannot just dump a JSON string inside SQL and expect it to be syntactically valid.

String literals in SQL go in 'single quotes' but then just dumping data in SQL i quotes exposes you to SQL injection. Use variables instead.

If you use SQLiteDatabse update() method with ContentValues , it uses variables automatically for you.

If you want to keep on working in raw SQL, change

String updateQuery="UPDATE "+OFFLINE_MODE +" SET "+OFFLINE_DATA+" = "+ data;
db.execSQL(updateQuery);

to something like

String updateQuery="UPDATE "+OFFLINE_MODE +" SET "+OFFLINE_DATA+" = ?";
db.execSQL(updateQuery, new Object[] { data });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM