SQLite 3 C API Transactions(SQLite 3 C API 事务)
问题描述
我正在使用 SQLite3 进行 iPhone 开发,并试图将一些插入语句包装到一个事务中.目前我有以下代码可以正常工作,但是在阅读了关于 SO 的另一个问题后,我意识到将这些代码放在一个事务中而不是每个事务中会更好.我找不到开始和提交事务的 C API 调用.部分代码使用 Objective-C,但我认为这与问题无关.
I am using SQLite3 for iPhone development and I'm attempting to wrap a few insert statements into a transaction. Currently I have the below code which is working properly however after reading another question on SO I realized it would be better to have these in one transaction rather than one each. I couldn't find the C API call to begin and commit a transaction. Some of the code is in Objective-C but I don't think that's really relevent to the question.
- (void)saveAnimals {
//Insert all the animals into the zoo database
int i;
const char *sql = "insert into Animal(Zoo_ID, Animal_Num, Animal_Text) Values(?, ?, ?)";
for (i = 0; i < ([[self animalArray] count] - 1); i++) {
if(addStmt == nil) {
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
int animalNum = [[[self animalArray] objectAtIndex:i] animal_Number];
NSString *animalText = [[NSString alloc] initWithString:[[[self animalArray] objectAtIndex:i] animal_Text]];
sqlite3_bind_int(addStmt, 1, zoo_ID);
sqlite3_bind_int(addStmt, 2, animalNum);
sqlite3_bind_text(addStmt, 3, [animalText UTF8String], -1, SQLITE_TRANSIENT);
[animalText release];
if(SQLITE_DONE != sqlite3_step(addStmt)) {
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
//Reset the add statement.
sqlite3_reset(addStmt);
}
}
我认为需要做的是将 sqlite3_prepare_v2 命令从 for 循环中取出,启动事务,通过 for 循环,提交事务.但是,我不确定开始交易"和提交交易"的要求是什么.我还会使用 sqlite3_step 吗?感谢您的帮助.
What I think needs to be done would be taking the sqlite3_prepare_v2 command out of the for loop, start the transaction, go through the for loop, commit the transaction. However, I'm not sure what the calls for "start the transaction" and "commit the transaction" are. And would I still use sqlite3_step? Thanks for your help.
推荐答案
Start a transaction with: sqlite3_exec(db, "BEGIN", 0, 0, 0);
Start a transaction with: sqlite3_exec(db, "BEGIN", 0, 0, 0);
提交一个事务:sqlite3_exec(db, "COMMIT", 0, 0, 0);
这篇关于SQLite 3 C API 事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQLite 3 C API 事务


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01