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