何时在 iOS 中的 sqlite 中执行 Finalize 语句?

发布于 2024-11-06 13:16:23 字数 125 浏览 0 评论 0原文

在iOS中使用sqlite时,什么时候执行finalize语句?

我需要在每次查询后完成语句吗?

重置 和 最终确定 和有什么不一样?

如果我重置了,我需要完成吗?

谢谢。

When using sqlite in iOS, when to do finalize statement ?

Do I need to finalize the statement after every query ?

What is the difference between reset and finalize ?

If I do reset, do I need to do finalize then ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-11-13 13:16:23

当您完全完成一条语句时,可以使用 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);

You use the sqlite3_finalize() function when you are completely finished with a statement, either because you did a one-off query like the following:

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);

or if you are done with a prepared statement that you'll never need again (possibly because you're cleaning up on exiting the application).

If you've created a statement that you'll want to use over and over again (for performance reasons, because it is reasonably expensive to prepare a statement), you use sqlite3_reset() to reset the query so that it is ready to use again. You don't want to finalize in that case until you have decided that you'll never want to reuse that statement (again, usually this is during the teardown of your application or a particular object).

An example of a query that only resets the statement at the end is as follows:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文