在 iOS 中使用 sqlite 时,什么时候做 finalize 语句?
是否需要在每次查询后完成语句?
reset 和 finalize 有什么区别?
如果我重置了,我还需要完成吗?
谢谢。
最佳答案
当您完全完成一条语句时,您可以使用 sqlite3_finalize()
函数,这可能是因为您执行了如下一次性查询:
const char *sql = "SELECT COUNT(*) FROM bonds WHERE molecule=? AND structure=?";
sqlite3_stmt *bondCountingStatement;
unsigned int totalBondCount = 0;
if (sqlite3_prepare_v2(database, sql, -1, &bondCountingStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_int(bondCountingStatement, 1, databaseKey);
sqlite3_bind_int(bondCountingStatement, 2, numberOfStructureBeingDisplayed);
if (sqlite3_step(bondCountingStatement) == SQLITE_ROW)
{
totalBondCount = sqlite3_column_int(bondCountingStatement, 0);
}
else
{
}
}
sqlite3_finalize(bondCountingStatement);
或者如果您完成了一条准备好的声明,您将不再需要它(可能是因为您在退出应用程序时正在清理)。
如果你创建了一个你想反复使用的语句(出于性能原因,因为准备一个语句的成本相当高),你可以使用 sqlite3_reset()
来重置查询,以便它可以再次使用。在您决定永远不想重用该语句之前,您不想在这种情况下完成(同样,这通常是在您的应用程序或特定对象的拆卸期间)。
仅在末尾重置语句的查询示例如下:
sqlite3_bind_text(insertMoleculeSQLStatement, 1, [filename UTF8String], -1, SQLITE_TRANSIENT);
int success = sqlite3_step(insertMoleculeSQLStatement);
// Because we want to reuse the statement, we reset it instead of finalizing it.
sqlite3_reset(insertMoleculeSQLStatement);
关于iphone - 什么时候在 iOS 的 sqlite 中完成语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5961492/