sqlite3_exec回调是同步还是异步

Is sqlite3_exec callback synchronous or asynchronous?

本文关键字:同步 异步 回调 exec sqlite3      更新时间:2023-10-16

Sqlite3 函数sqlite3_exec()回调参数是同步运行还是异步运行?

调用示例:

int retStatus = sqlite3_exec(pDB, "SELECT * FROM SomeTable WHERE Something='Hi'", callback, &object, &error);
...Next line(s) of code...
  • 同步:执行sqlite3_exec行,然后调用回调,然后执行下一行代码。

  • 异步:执行sqlite3_exec行,下一行代码执行,并在某个时候调用回调。

同步。在代码继续之前,为找到的每一行调用回调:

static int _getName(void* pNames, int columns, char** data, char** columnNames)
{
    if (columns < 2)
    {
        assert(false && "Function called on wrong table!");
        // Signify failure
        return 1;
    }
    std::vector<std::string>* vNames = static_cast< std::vector<std::string>* >(pNames);
    vNames->push_back(data[1]);
    // Success:
    return 0;
}
{
    std::vector<std::string> names;
    // assume pDB is a valid, opened sqlite3* db
    char* error = 0;
    if (sqlite3_exec(pDB, "SELECT * FROM TableNames", _getName, static_cast<void*>(&names), &error) != SQLITE_OK)
    {
        // log and free error:
    }
    assert(names.size() && "No entries were found in the table");
}

将在表TableNames中找到的每个条目上调用_getName。如果未找到任何条目,则不会调用该函数,并且不会有错误。如果您有名为 TableNames 的表,其中包含 10 个条目,则names.size() == 10 。 如果返回非零_getName,将不再调用