I’m trying to update a specific row by ID :

fun updateEmployeeInfo(id:Int, firstName:String): Int {
    val db = this.writableDatabase
    var cv = ContentValues()

    cv.put(COL_FIRSTNAME, firstName )
    val result = db.update(TABLE_NAME, cv, COL_FIRSTNAME+"=?", arrayOf(firstName))
    return result

Running this with an ID that already exists in the database it isn’t updating.

screenshot of the database

You are saying update the rows with the provided (passed) first name by changing the first name to the very same first name (effectively doing nothing).

I believe that you want to use:-

val result = db.update(TABLE_NAME, cv, COL_ID+"=?", arrayOf(id.toString))
  • assuming that COL_ID holds the value of the id column name.

  • or you could use the more concise return db.update(TABLE_NAME, cv, COL_ID+"=?", arrayOf(id.toString))

This is then saying update the row where the id is the provided/passed id changing the first name, whatever it is, to the provided/passed first name.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *