Buneme Kyakilika
Buneme Kyakilika

Reputation: 1202

Android SQLite update row not working

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);

}

Upvotes: 2

Views: 4899

Answers (1)

Chintan Soni
Chintan Soni

Reputation: 25287

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.

Upvotes: 4

Related Questions