我需要更新db表的某些单元格。我有一个更新某个值的函数,这里是:
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
}
我看过db.update(String table, ContentValues values, String whereClause, String[] whereArgs)
。它返回受影响的行,但是如何使用同一行的另一个单元更新某个单元格而不替换它?
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)});
我怎样才能使用更新方法,但之前没有查询?
答案 0 :(得分:1)
update()
方法仅支持字面值。
为了能够使用旧值进行计算,您必须继续使用execSQL()
。