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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With