在 sqflite 中使多个操作原子化
我正在扑动中创建一个移动应用程序,需要在交易表中进行条目,还需要更新用户表中的余额。每当要添加新事务时,我都会首先检查用户的当前余额,在交易表中添加条目并更新余额。我的问题是如何使整个Atomic进行整个操作。假设更新余额失败,因为出于任何原因,我还能从交易表中退回。
I am creating a mobile application in flutter where I need to do entries in the transaction table and also need to update the balance in the user table. Whenever a new transaction is to be added I first check the current balance of the user, add the entry in the transaction table and update the balance. My question is how can I make the entire operation atomic. Say if the update balance fails because for any reason how can I roll back from the transaction table also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这正是 SQLite 事务 的用途! (很抱歉您的表名称不幸,也称为事务)有关
如何在 sqflite 中使用事务的更多信息,请参见:
https://github.com/tekartik/sqflite/blob/master/sqflite/doc/sql.md#transaction
为了方便起见,复制/粘贴了一些信息:
事务
transaction
句柄“全有或全无”的场景。如果一个命令失败(并引发错误),则所有其他命令都会恢复。txn
- 在事务中使用(使用db
对象本身会导致死锁),That is exactly what SQLite transactions are for! (Sorry for the unfortunate name of your table which is also called transaction)
More info on how to use transactions in sqflite here:
https://github.com/tekartik/sqflite/blob/master/sqflite/doc/sql.md#transaction
some copy/pasted information for convenience:
transaction
transaction
handle the 'all or nothing' scenario. If one command fails (and throws an error), all other commands are reverted.txn
in the code above - is used in a transaction (using thedb
object itself will cause a deadlock),