12
我正在使用SQLite3进行iPhone开发,并试图将一些插入语句包装到事务中。目前我有下面的代码工作正常,但是在阅读另一个问题后,我意识到将这些事务放在一个事务中而不是每个事务中都会更好。我无法找到C API调用来开始和提交事务。一些代码在Objective-C中,但我不认为这是真正相关的问题。SQLite 3 C API交易
- (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?谢谢你的帮助。 sqlite3_exec(db, "BEGIN", 0, 0, 0);
提交与交易:
你会觉得会有这种更清洁的API ......为什么他们不只是包括sqlite3_exec_trans_begin(DB)调用... – klynch 2010-03-28 06:25:40