c - 如何完成所有待处理的 SELECT 语句?

标签 c sqlite

SQLite wiki page for "database is locked"说:

Sometimes people think they have finished with a SELECT statement because sqlite3_step() has returned SQLITE_DONE. But the SELECT is not really complete until sqlite3_reset() or sqlite3_finalize() have been called. As of check-in [3902] (2007-05-02 after version 3.3.17) this is now allowed for CREATE statement.

当然,我可以关闭并重新打开数据库,但这将使所有准备好的语句无效。是否存在另一种方法,不需要我跟踪所有待处理的 SELECT 语句?

最佳答案

I can, of course, close and reopen the database, but this would invalidate all prepared statements. Does there exist another way that would not involve me tracking all pending SELECT statements?

SQLite 已经跟踪您所有准备好的语句,并且它提供 sqlite3_next_stmt()作为一种可以遍历其列表的机制。还有sqlite3_stmt_busy()通过它,您可以测试语句是否使事务处于打开状态,但这并不能帮助您识别已运行完成的语句,因此与实时事务无关,但仍然有数据库资源分配给它们尚未最终确定或重置。

这里有一种方法可以重置 SQLite 当前已知的所有准备好的语句:

    sqlite3 *db = /* ... */
    sqlite3_stmt *stmt;

    /* ensure that there is no open transaction; will fail harmlessly if
       there already is none */
    sqlite3_exec(db, "rollback", NULL, NULL, NULL);

    /* Clean up any outstanding prepared statements */
    for (stmt = sqlite3_next_stmt(db, NULL); stmt; stmt = sqlite3_next_stmt(db, stmt)) {
        int result = sqlite3_reset(stmt);
        /* handle errors ... */
    }

重置不需要的语句应该不会有害。

话虽如此,您应该跟踪当前运行的语句,并确保在完成它们提供的当前结果集后重置它们。这需要一些纪律,但这是正确的做法。

关于c - 如何完成所有待处理的 SELECT 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36628219/

相关文章:

c# - 在不同的应用程序线程上同时写入多个 SQLite 数据库的并发问题

c - 二维数组元素设置后发生变化

c - 使用 libcurl 上传 C 字符串

sql - 如何进行分组的多表SQL连接

sql - 如何判断 sqlite 列是否为 AUTOINCREMENT?

javascript - alasql-记录更新问题

c - GNU C 运行时库中的哪些函数在标准库中?

C Makefile - 不同文件结尾的子目录

C 程序在执行 execlp 函数时退出

android - 如何使用数据库编写 Robolectric (2.3) 测试