gpt4 book ai didi

android - 如何根据另一行的更改更新一行?

转载 作者:行者123 更新时间:2023-11-29 18:31:24 24 4
gpt4 key购买 nike

假设我有两个表来跟踪银行账户:

Account:
------------------------------
AccountID| Account Value
------------------------------
| 0 | 1000

Transactions:
------------------------------------------------
TransactionID| Transaction Value| Paying Account
-------------------------------------------------
| 23 | -50.0 | 0
--------------------------------------------------
| 24 | -5 | 0

TransactionID=24 有一个更新,新值是-10。更新账户值(value)以反射(reflect)这一变化的最佳方式是什么?

我在代码中所做的是

  1. 在编辑对话框中点击确定后
  2. 通过添加负值来反转原始交易:
  3. 然后将新的交易值(value)应用到账户

    AccountDAO acc = mDatabase.AccountDAO().getAccount(0);
    acc.account_value += transaction_id_24.value * -1;
    acc.account_value += transaction_id_24_updated.value;
    mDatabase.AccountDAO().updateAccount(acc);

    上述方法可行,但我觉得有比上述方法更好的自动化方法。

有没有办法让数据库自动检测到某笔交易的金额发生变化,然后更新sqlite/android-room中对应的账户?

最佳答案

Room 内置了您需要的所有工具:

Use LiveData with Room

The Room persistence library supports observable queries, which return LiveData objects. Observable queries are written as part of a Database Access Object (DAO).

Room generates all the necessary code to update the LiveData object when a database is updated. The generated code runs the query asynchronously on a background thread when needed. This pattern is useful for keeping the data displayed in a UI in sync with the data stored in a database. You can read more about Room and DAOs in the Room persistent library guide.

只需在 Room 中观察从您 @Query 返回的 LiveData,您可以使用返回的结果执行其他逻辑。

关于android - 如何根据另一行的更改更新一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com