我正在为 SQLite API 编写一个轻型包装器。
基本上,我很好奇如何/何时执行 SQLite 预编译语句...
我去的时候:
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);
真正的sql是在什么时候执行的?是在调用 sqlite3_prepare_v2
期间,还是在第一个 sqlite3_step
期间?
非常感谢任何清晰度:)
干杯
贾勒特
最佳答案
根据SQLite documentation for sqlite3_prepare
,我们看到:
To execute an SQL query, it must first be compiled into a byte-code program using one of these routines: sqlite3_prepare, sqlite3_prepare_v2, sqlite3_prepare16, sqlite3_prepare16_v2.
对于 sqlite3_step
:
After a prepared statement has been prepared using either sqlite3_prepare_v2() or sqlite3_prepare16_v2() or one of the legacy interfaces sqlite3_prepare() or sqlite3_prepare16(), this function must be called one or more times to evaluate the statement.
更多信息:
SQLite 有一个虚拟机,可以执行所有必要的操作来在选定的数据库上执行代码。 sqlite3_prepare
(及其系列)将您的 SQL 语句编译成可以在此虚拟机上执行的字节码。另一方面,sqlite3_step
在 VM 中执行该字节码。
关于c++ - sqlite编译语句什么时候得到 'executed',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12978133/