Oleksandr Albul Oleksandr Albul - 28 days ago 9
Android Question

How to update a row using old values without a query in SQLite?

I need to update certain cells of db table. I have a function that updates certain value, here it is:

public final int updateBpm(long millisOfDay, float bpmValue, int trngTime, int id) {
mDb.execSQL("UPDATE " + TABLE_BPM_STAT +
" SET " + BPM_COLUMN + " = (" + BPM_COLUMN + " * " + TIME_COLUMN + " + "
+ bpmValue + " * " + trngTime + ") / (" + TIME_COLUMN + " + " + trngTime + "),"
+ TIME_COLUMN + " = " + TIME_COLUMN + " + " + trngTime + ", "
+ " WHERE " + DATE_COLUMN + " = " + millisOfDay +
" AND " + ID_COLUMN + " = " + id);
// here if affected rows == 0, I should insert this value
}


I've looked at
db.update(String table, ContentValues values, String whereClause, String[] whereArgs)
. It returns affected rows, but how can I update certain cell using another cells of the same row, without replacing it?

final ContentValues updateValues = new ContentValues();
updateValues.put(BPM_COLUMN, ..); // I don't need replace it, I need to use old data from the table to construct a new one
updateValues.put(TIME_COLUMN, ..);
mDb.update(STAT_TABLE, updateValues, ID_COLUMN + " = ?", new String[]{String.valueOf(id)});


How can I do it using update method, but without query before it?

CL. CL.
Answer

The update() method supports only literal valued. To be able to use old values for computations, you have to keep using execSQL().