gpt4 book ai didi

sql - 我不完全理解 sqlite3_finalize

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:19:51 24 4
gpt4 key购买 nike

我不完全理解sqlite3_finalize。我的初始代码。

while (j < Autors.count)
{
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_int(compiledStatement, 1, ((AuthorSettings *)[Autors objectAtIndex:j]).authorId);
if(sqlite3_step(compiledStatement) != SQLITE_DONE)
{
NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database));
sqlite3_busy_timeout(database, 5);
}
else
{
if(sqlite3_prepare_v2(database, sqlStatement_1, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_int(compiledStatement, 1, bookId);
if(sqlite3_step(compiledStatement) != SQLITE_DONE)
{
NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database));
sqlite3_busy_timeout(database, 5);
}
else j++;
}
else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));
}
}
}
sqlite3_finalize(compiledStatement);

我添加了 sqlite3_finalize 函数。请检查我。

while (j < Autors.count)
{
sqlite3_finalize(compiledStatement); //**added
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_int(compiledStatement, 1, ((AuthorSettings *)[Autors objectAtIndex:j]).authorId);
if(sqlite3_step(compiledStatement) != SQLITE_DONE)
{
NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database));
sqlite3_busy_timeout(database, 5);
}
else
{
sqlite3_finalize(compiledStatement); //**added
if(sqlite3_prepare_v2(database, sqlStatement_1, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_int(compiledStatement, 1, bookId);
if(sqlite3_step(compiledStatement) != SQLITE_DONE)
{
NSLog(@"sqlite3_step error %s", sqlite3_errmsg(database));
sqlite3_busy_timeout(database, 5);
}
else j++;
}
else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));
}
}
}
sqlite3_finalize(compiledStatement);

最佳答案

sqlite3_finalizesqlite3_prepare_v2 相反。所以你的代码应该是这样的:

sqlite3_prepare_v2(...);
while () {
sqlite3_reset(...);
sqlite3_bind_int(...);
sqlite3_step(...);
}
sqlite3_finalize(...);

你不想准备不必要的陈述。

关于sql - 我不完全理解 sqlite3_finalize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10208171/

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