gpt4 book ai didi

iphone - iOS - SQLite 批量插入错误

转载 作者:行者123 更新时间:2023-11-29 11:06:06 26 4
gpt4 key购买 nike

我需要向我的数据库中插入大量记录,但出现错误:

Prepare-error library routine called out of sequence

这是我的代码:

NSString *databaseName = @"DB.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

sqlite3 *concertsDB;
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &concertsDB) == SQLITE_OK)
{
sqlite3_exec(concertsDB, "BEGIN TRANSACTION", 0, 0, 0);
const char *sqlStatement = "INSERT INTO concertsData VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
sqlite3_stmt *compiledStatement;

if (sqlite3_prepare_v2(concertsDB, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
int hasError;

for (int i=0; i<[events count]; i++) {

sqlite3_bind_text(compiledStatement, 1, [[[events objectAtIndex:i] title] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compiledStatement, 2, [[[events objectAtIndex:i] date] timeIntervalSince1970]);

sqlite3_bind_text(compiledStatement, 3, [[[events objectAtIndex:i] time] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 4, [[[events objectAtIndex:i] shortDesription] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 5, [[[events objectAtIndex:i] conductor] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 6, [[[events objectAtIndex:i] location] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 7, [[[events objectAtIndex:i] durations] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 8, [[[events objectAtIndex:i] works] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 9, [[[events objectAtIndex:i] solists] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 10, [[[events objectAtIndex:i] fulltext] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 11, [[[[events objectAtIndex:i] concertUrl] absoluteString] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 12, [[[[events objectAtIndex:i] buyUrl] absoluteString] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(compiledStatement, 13, [[[events objectAtIndex:i] imageName] UTF8String], -1, SQLITE_TRANSIENT);

if (sqlite3_step(compiledStatement) != SQLITE_DONE) {
hasError=1;
NSLog(@"Prepare-error %s", sqlite3_errmsg(concertsDB));
}

sqlite3_clear_bindings(compiledStatement);
}
sqlite3_reset(compiledStatement);
if( hasError == 0 ) {
sqlite3_exec(concertsDB, "COMMIT", 0, 0, 0);
}
else {
sqlite3_exec(concertsDB, "ROLLBACK", 0, 0, 0);
}

}

sqlite3_close(concertsDB);
}

谁能告诉我哪里出了问题?

最佳答案

将对 sqlite3_clear_bindings 的调用替换为对 sqlite3_reset 的调用。然后将当前对 sqlite3_reset 的调用替换为对 sqlite3_finalize 的调用。

sqlite3_reset 如果您需要再次重用该语句,则必须在调用 sqlite3_step 之后调用。

sqlite3_finalize 必须在语句完全完成后调用。

不需要调用 sqlite3_clear_bindings,因为您在每个循环迭代中设置了每个绑定(bind)变量。

关于iphone - iOS - SQLite 批量插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13551816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com