I wrote an application to update rows in SQlite Db, below is the code for update in data handler:
public long updatedata(String ID, String NAME,String NUMBER)
{
ContentValues content = new ContentValues();
content.put(name, NAME);
content.put(number,NUMBER);
return db.update(table_name, content, "id "+"="+ID, null);
}
now when i need to update a row i used below code:
Datahandler data =new Datahandler();
data.open();
long updateresult = data.updatedata(oldID, cloudnames.get(pos), cloudnumbers.get(pos));
data.close();
Also assume that the above provided value is exist and valid and also i am sure that there is an id = jo_2 in my database, the problem is when i run the code an exception appear as below:
02-19 23:59:34.200: E/SQLiteLog(18485): (1) no such column: jo_2
02-19 23:59:34.205: W/dalvikvm(18485): threadid=11: thread exiting with uncaught exception (group=0x41b30c08)
02-19 23:59:34.210: E/AndroidRuntime(18485): FATAL EXCEPTION: IntentService[test-service]
02-19 23:59:34.210: E/AndroidRuntime(18485): Process: com.kaizendroid.bc, PID: 18485
02-19 23:59:34.210: E/AndroidRuntime(18485): android.database.sqlite.SQLiteException: no such column: jo_2 (code 1): , while compiling: UPDATE info SET name=?,number=? WHERE id =jo_2
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1706)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1654)
02-19 23:59:34.210: E/AndroidRuntime(18485): at com.kaizendroid.bc.DataHandler.updatedata(DataHandler.java:122)
02-19 23:59:34.210: E/AndroidRuntime(18485): at com.kaizendroid.bc.MyTestService.getbcfromcloud(MyTestService.java:169)
02-19 23:59:34.210: E/AndroidRuntime(18485): at com.kaizendroid.bc.MyTestService.get_local_cloud_compare(MyTestService.java:70)
02-19 23:59:34.210: E/AndroidRuntime(18485): at com.kaizendroid.bc.MyTestService.onHandleIntent(MyTestService.java:49)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.os.Handler.dispatchMessage(Handler.java:102)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.os.Looper.loop(Looper.java:146)
02-19 23:59:34.210: E/AndroidRuntime(18485): at android.os.HandlerThread.run(HandlerThread.java:61)
The error isn't saying that a value of "jo_2" is missing in the table, but that a column named jo_2 is missing. It sounds like you have some malformed SQL somewhere.
If this is your actual SQL (from the error):
UPDATE info SET name=?,number=? WHERE id =jo_2
Then you need some quotes around the jo_2 part:
UPDATE info SET name=?,number=? WHERE id = 'jo_2'
Without them, SQLite is comparing the column id to the column jo_2, which doesn't exist.
To fix this, you can change this line in your code:
return db.update(table_name, content, "id = '" + ID + "'", null);
// add quotes here ^ and ^
EDIT: Falmarri's answer shows a safer way to fix your code to avoid the error.
You're opening yourself up to SQL injection. Use the built in where clause parameter inserting.
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
So you want
return db.update(table_name, content, "id = ?", new String[] {"jo_2"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With