I’m writing a light wrapper for the SQLite API.
Basically, I’m curious about how/when an SQLite pre-compiled statement gets executed…
When i go:
char buffer[] = "INSERT INTO example VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(mDb, buffer, strlen(buffer), &stmt, NULL);
for (unsigned i = 0; i < mVal; i++)
{
std::string id = getID();
sqlite3_bind_text(stmt, 1, id.c_str(), id.size(), SQLITE_STATIC);
sqlite3_bind_double(stmt, 2, getDouble());
sqlite3_bind_double(stmt, 3, getDouble());
sqlite3_bind_double(stmt, 4, getDouble());
sqlite3_bind_int(stmt, 5, getInt());
sqlite3_bind_int(stmt, 6, getInt());
sqlite3_bind_int(stmt, 7, getInt());
if (sqlite3_step(stmt) != SQLITE_DONE)
{
printf("Commit Failed!\n");
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
At what point is the actual sql executed? Is it during the call to sqlite3_prepare_v2, or during the first sqlite3_step?
Any clarity is greatly appreciated 🙂
Cheers
Jarrett
According to the SQLite documentation for
sqlite3_prepare, we see:And for
sqlite3_step:More information:
SQLite has a virtual machine that does all necessary actions to execute your code on the selected database.
sqlite3_prepare(and its family) compiles your SQL statement into byte code that can be executed on this virtual machine. On the other hand,sqlite3_stepexecutes that byte-code in the VM.