How to commit a SQLite transaction in C/C++?

二次信任 提交于 2019-12-21 07:22:13

问题


I am using sqlite c/c++ interface.

I have 3 tables (related tables) say A, B, C. Now, there is a function called Set, which get some inputs and based on the inputs inserts rows into these three tables. (sometimes it can be an update in one of the tables)

Now I need two things. One, i dont want autocommit feature. Basically I would like to commit after every 1000 calls to Set function

Secondly, within the set function itself, if i find that after inserting into two tables, the third insert fails, then i have to revert, those particular changes in that Set function call.

Now i don't see any sqlite3_commit function exposed. I only see a function called sqlite3_commit_hook() which is slightly diff in documentation.

Are there any function exposed for this purpose? or What is the way to achieve this behaviour?

Can you help me with the best approach of doing this.


回答1:


can you use BEGIN, COMMIT and ROLLBACK sql statements inside the C/C++ code?




回答2:


You use sqlite3_exec and pass "BEGIN TRANSACTION" and "END TRANSACTION" respectively.

// 'db' is the pointer you got from sqlite3_open*
sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
// Any (modifying) SQL commands executed here are not committed until at the you call:
sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);

There are synonyms for these SQL commands (like COMMIT instead of END TRANSACTION). For reference, here is the SQLite documentation for transactions.



来源:https://stackoverflow.com/questions/2880149/how-to-commit-a-sqlite-transaction-in-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!