简体   繁体   English

如何使用旧值更新行而不在SQLite中进行查询?

[英]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) . 我看过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? 如何使用更新方法来执行此操作,但是之前没有查询?

The update() method supports only literal valued. update()方法仅支持文字值。 To be able to use old values for computations, you have to keep using execSQL() . 为了能够使用旧值进行计算,您必须继续使用execSQL()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM