[英]How to return sum from Room database?
我需要從Expense對象獲取value字段的總和,並在textview中顯示它,但我真的不知道如何在Room Database中做到這一點。 我需要在Dao中創建特定的查詢,還是有其他方法?
Pojo:
@Entity(tableName = "expense_table")
public class Expense {
@PrimaryKey
private int id;
private String note;
private Double value;
private String type;
道:
@Dao
public interface ExpenseDao {
@Insert
void insertExpense(Expense expense);
@Query("SELECT * FROM expense_table")
LiveData<List<Expense>> getExpensesByDay();
您需要在DAO中創建一個新查詢
@Query("SELECT COALESCE(sum(COALESCE(value,0)), 0) From expense_table")
LiveData<Double> getTotal();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.