2

I am trying to update a row in my SQLite database. The update row method in my Database Helper class is as follows

public long updateNote(long rowId, String title, String body) {
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    args.put(KEY_BODY, body);

    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" +  rowId,
            null);
}

And the row is updated like this:

     mDbHelper.open();
     mDbHelper.updateNote(mRowId, title, body);
     mDbHelper.close();

But when I retrieve the rows, the data is exactly the same as before. I've searched SO already but I couldn't find any answers. The data is retrived in a cursor like this:

public Cursor fetchAllNotes() {
    return mDb.query(DATABASE_TABLE,
            new String[] { KEY_ROWID, KEY_TITLE, KEY_BODY, "link", "type",
                    KEY_DATE, KEY_TIME, KEY_DAY, KEY_YEAR }, null, null,
            null, null, null);

}
Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34

1 Answers1

4

try changing the method like:

public long updateNote(long rowId, String title, String body) {
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    args.put(KEY_BODY, body);

    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=?",
            new String[]{rowId+""});
}

Hope this works.

eternay
  • 3,754
  • 2
  • 29
  • 27
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • It's better to use parameterized queries because database can optimize them for better performance, but it won't change anything. – eternay May 29 '13 at 16:02
  • +1 it works now ( been stuck on that for days), but I had to do "rowId = rowId +1;" before the ContentValues statment. you should edit your answer to include that for others, Thanks :D – Buneme Kyakilika May 29 '13 at 16:07