I'm fairly new to SQLite, and I'm just trying to get my head around running transactions (with the C API). In this case, I just need to run two consistent updates on two tables.
The easiest approach seems to be the one in this tutorial. You just sqlite3_exec
a single combined sequence of commands, which start with BEGIN
and end with COMMIT
. So, you never do a ROLLBACK
, and you presumably rely on SQLite automatically rolling back if it encounters an error.
The problem is that the section on handling transaction errors in the manual is fairly complex, and it's not obvious to me that this is a good approach. The doc also suggests manually rolling back.
The next approach would be to exec a single BEGIN
, and then run each statement individually, checking for errors, and then finally run a COMMIT
or ROLLBACK
. Is this actually any better, or is it just busywork?
答案 0 :(得分:0)
sqlite3_exec()
将在遇到的第一个错误中止,大多数错误都不会导致自动回滚。
首先执行BEGIN,然后执行事务中的所有内容,然后使用COMMIT或ROLLBACK结束事务。
在ROLLBACK的情况下,您可能只是忽略任何错误。转换已经回滚,或者你无论如何也无法做到。