Is a synchronous or asynchronous call to sqlite3_exec?

Does Sqlite3 function sqlite3_exec()with callback parameters work synchronously or asynchronously?

Call example:

int retStatus = sqlite3_exec(pDB, "SELECT * FROM SomeTable WHERE Something='Hi'", callback, &object, &error);

...Next line(s) of code...
  • Synchronous: the sqlite3_exec line is executed, then the callback is called, then the next line (s) of code is executed.

  • Asynchonous: the sqlite3_exec line is executed, the next line (s) of code, and at some point a callback is called.

+4
source share
1 answer

Synchronous. A callback is called for each line found before continuing the code:

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

_getName , TableNames. , , . TableNames, 10 , names.size() == 10. _getName ,

+5

All Articles