Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sqlite3_exec callback synchronous or asynchronous?

Does the Sqlite3 function sqlite3_exec() with a callback parameters runs synchonously or asynchronously ?

Example call:

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

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

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

like image 316
Mendes Avatar asked May 01 '15 01:05

Mendes


1 Answers

Synchronous. The callback is called for each row found before code continues:

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 will be called on every entry found in the table TableNames. If no entries are found, the function won't be called, and there will be no error. If you have table called TableNames that has 10 entries, then names.size() == 10. _getName will no longer be called if it returns non-zero

like image 161
Tas Avatar answered Nov 01 '22 00:11

Tas