【问题标题】:when does an sqlite compiled statement get 'executed'sqlite 编译语句何时“执行”
【发布时间】:2012-10-19 16:02:39
【问题描述】:

我正在为 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 期间?

非常感谢任何清晰:)

干杯

贾勒特

【问题讨论】:

  • 在绑定参数之前,SQL 不能执行。 prep 正在编译代码模板,step 正在执行。

标签: c++ sql sqlite


【解决方案1】:

根据SQLite documentation for sqlite3_prepare,我们看到:

要执行 SQL 查询,首先必须使用以下命令将其编译成字节码程序 这些例程之一: sqlite3_prepare、sqlite3_prepare_v2、sqlite3_prepare16、sqlite3_prepare16_v2。

对于sqlite3_step

在使用 sqlite3_prepare_v2() 或 sqlite3_prepare16_v2() 或旧接口之一 sqlite3_prepare() 或 sqlite3_prepare16(),这个函数必须被调用一次或多次才能评估 声明。

更多信息:

SQLite 有一个虚拟机,它执行所有必要的操作来在选定的数据库上执行您的代码。 sqlite3_prepare(及其家族)将您的 SQL 语句编译成可以在此虚拟机上执行的字节码。另一方面,sqlite3_step 在 VM 中执行该字节码。

【讨论】:

  • 哈哈,你去 - 谢谢你指出我@CL :)
猜你喜欢
  • 2021-10-20
  • 2013-04-15
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多