[英]How to update a row using old values without a query in SQLite?
我需要更新數據庫表的某些單元格。 我有一個更新某些值的函數,這里是:
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)});
如何使用更新方法來執行此操作,但是之前沒有查詢?
update()
方法僅支持文字值。 為了能夠使用舊值進行計算,您必須繼續使用execSQL()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.