如何在C / C ++中提交SQLite事务? [英] How to commit a SQLite transaction in C/C++?

查看:135
本文介绍了如何在C / C ++中提交SQLite事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sqlite c / c ++接口。

I am using sqlite c/c++ interface.

我有3个表(相关表),分别是A,B,C。现在,有一个名为 Set ,它获得一些输入,并根据输入将行插入这三个表中。 (有时可能是其中一个表的更新)

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)

现在我需要两件事。一,我不想自动提交功能。基本上,我想在每1000次调用 Set 函数

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

其次,如果我在插入到两个表,第三个插入操作失败,然后我必须还原该 Set 函数调用中的那些特定更改。

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.

现在我不查看任何公开的sqlite3_commit函数。我只看到一个名为sqlite3_commit_hook()的函数,该函数在文档中略有不同。

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.

推荐答案

您使用 sqlite3_exec 并传递 BEGIN

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);

这些SQL命令有同义词(例如 COMMIT ,而不是 END TRANSACTION )。作为参考,这是用于交易的SQLite文档

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

这篇关于如何在C / C ++中提交SQLite事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆