Rishabh Mehrotra February 2016

Update row in database android

public int updateContact(Student student) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, student.getNAME());
    values.put(KEY_ADD, student.getADD());

    // updating row
    return db.update(TABLE_STUDENTS, values, KEY_ID +" = ?",
            new String[] { String.valueOf(student.getID()) });
}

Answers


RazorOne February 2016

Your source code is generally correct. The ID is a question mark but you can resolve that like this (if that's what you would like):

public boolean updateContact(long id, Student student) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(KEY_NAME, student.getNAME());
    contentValues.put(KEY_ADD, student.getADD());

    return db.update(list, contentValues, KEY_ID + "=" + id, null) > 0;
}

This should update the row. Also, you should create a DatabaseHelper so that you can have multiple functions in your Database class accessing one instance of the SQLite Database. When calling your function, pass the row id as a parameter.

Post Status

Asked in February 2016
Viewed 1,271 times
Voted 11
Answered 1 times

Search




Leave an answer