Mysql
 SQL >> Database >  >> RDS >> Mysql

Sommare un valore in un database Android Room

Aggiungi un metodo DAO per incrementare un valore:

@Query("UPDATE sum SET value = value + :addValue WHERE id = :id")
suspend fun updateSum(id: Int, addValue: Long)

Per gestire insert o update in modo atomico:

@Transaction
suspend fun updateOrInsertSum(sum: Sum) {
  getSumById(sum.id)?.let {
    updateSum(sum.id, sum.value)
  } ?: insertSum(sum)
}