Skip to content Skip to sidebar Skip to footer

Sqlitedatabase Update Not Working?

I am trying to get db.update to update one of the rows of my database with new values but it does not seem to be saving. I have looked over my syntax but I cannot seem to get the d

Solution 1:

Would you print the db.update(TABLE_NAME, values, C_ID +"="+id, null) function return value?

If the return value is 0, so there is no such "id" row record in DB.

Solution 2:

I can't see the way how you create a table "tasks" and its attributes. However if you update all the attributes you might try to use replace() method. Try to replace your

publicvoidupdate(int id, ToDoItem task){
...
}

with:

publicvoidupdate(int id, ToDoItem task) {
    ContentValuesvalues=newContentValues();
    values.put(C_TASK, task.getTask());
    values.put(C_PRIORITY, task.getPriority());
    db.replace(TABLE_NAME, null, initialValues);
}

Also make sure that task.getTask() returns correct integer value that exists in your "tasks" table. You call update() method with the int parameter however you never use it in your update method. In your update method you might want to replace:

values.put(C_TASK, task.getTask());

with

values.put(C_TASK, id);

Solution 3:

I solved this problem I did not have C_ID reading in when the task was created therefore I changed the "integer primary key" when the database was create to "integer primary key autoincrement" which meant that update could now update the correct row. Thank you everyone who helped me with this issue.

Post a Comment for "Sqlitedatabase Update Not Working?"