在SQLite3中绑定参数是如何工作的(最小示例)

How does binding parameters work in SQLite3 (with minimal example)?

本文关键字:SQLite3 工作 参数 绑定 何工作      更新时间:2023-10-16

有人建议在SQLite中使用参数绑定来加速重复查询。但是,如果我有多个要绑定的参数,它就不起作用了。我没有看到我的错误。所有SQLite函数返回SQLITE_OK…

下面,我写了一个最小的例子。它创建一个表,创建三个条目,然后查询两次,一次使用一个绑定参数,一次使用两个绑定参数。第一次返回一个结果,这是正确的。第二个返回零结果,而sqlite3_step()返回101 (SQLITE_DONE)。为什么它也不返回一个结果?
#include <vector>
#include <sqlite3.h>
int main(int argc, char * argv[])
{
  sqlite3 * pDB;
  int iReturn = sqlite3_open("./Test.db", &pDB);
  if (iReturn != SQLITE_OK) {
    return 1;
  }
  // create table
  {
    std::string str = "CREATE TABLE Names(ID INTEGER PRIMARY KEY, Name VARCHAR(30))";
    iReturn = sqlite3_exec(pDB, str.c_str(), NULL, NULL, NULL);
    if (iReturn != SQLITE_OK) {
      sqlite3_close(pDB);
      return 1;
    }
  }
  // insert data using binding
  {
    std::string str = "INSERT INTO Names(Name) VALUES(?)";
    sqlite3_stmt * pStmt = nullptr;
    iReturn = sqlite3_prepare_v2(pDB, str.c_str(), str.size() + 1, &pStmt, nullptr);
    if (iReturn != SQLITE_OK) {
      sqlite3_close(pDB);
      return 1;
    }
    printf("The statement %s has %d parameter(s).n", str.c_str(), sqlite3_bind_parameter_count(pStmt));
    std::vector<std::string> vecNames;
    vecNames.push_back("Smith");
    vecNames.push_back("Morpheus");
    vecNames.push_back("Neo");
    for (unsigned int i = 0, iEnd = vecNames.size(); i != iEnd; ++i)
    {
      iReturn = sqlite3_bind_text(pStmt, 1, vecNames[i].c_str(), vecNames[i].size() + 1, nullptr);
      if (iReturn != SQLITE_OK) {
        return 1;
      }
      if (sqlite3_step(pStmt) != SQLITE_DONE) {
        sqlite3_finalize(pStmt);
        sqlite3_close(pDB);
        return 1;
      }
      sqlite3_reset(pStmt);
      sqlite3_clear_bindings(pStmt);
    }
  }
  // query using one bind parameter
  {
    sqlite3_stmt * pStmt = nullptr;
    string str = "SELECT ID FROM Names WHERE Name=?1";
    iReturn = sqlite3_prepare_v2(pDB, str.c_str(), str.size() + 1, &pStmt, nullptr);
    if (iReturn != SQLITE_OK) {
      return 1;
    }
    printf("The statement %s has %d parameters(s).n", str.c_str(), sqlite3_bind_parameter_count(pStmt));
    // fourth parameter is length = position of 
    iReturn = sqlite3_bind_text(pStmt, 1, "Neo", 3, NULL);
    if (iReturn != SQLITE_OK) {
      return 1;
    }
    vector<string> vecResults;
    char cBuffer[1024];
    string strBuffer;
    while (sqlite3_step(pStmt) == SQLITE_ROW)
    {
      sprintf(cBuffer, "%s", sqlite3_column_text(pStmt, 0));
      strBuffer = cBuffer;
      vecResults.push_back(strBuffer);
    }
    sqlite3_finalize(pStmt);
    printf("Found %d results.n", vecResults.size());
    for (unsigned int i = 0, iEnd = vecResults.size(); i != iEnd; ++i)
    {
      printf("%d: %sn", i, vecResults[i].c_str());
    }
  }
  // query using two bind parameters
  {
    sqlite3_stmt * pStmt = nullptr;
    string str = "SELECT ID FROM Names WHERE Name=?1 AND ID=?2";
    iReturn = sqlite3_prepare_v2(pDB, str.c_str(), str.size() + 1, &pStmt, nullptr);
    if (iReturn != SQLITE_OK) {
      return 1;
    }
    printf("The statement %s has %d parameters(s).n", str.c_str(), sqlite3_bind_parameter_count(pStmt));
    // fourth parameter is length = position of 
    iReturn = sqlite3_bind_text(pStmt, 1, "Neo", 3, NULL);
    if (iReturn != SQLITE_OK) {
      return 1;
    }

    iReturn = sqlite3_bind_text(pStmt, 2, "3", 2, NULL);
    if (iReturn != SQLITE_OK) {
      return 1;
    }

    vector<string> vecResults;
    char cBuffer[1024];
    string strBuffer;
    while (sqlite3_step(pStmt) == SQLITE_ROW)
    {
      sprintf(cBuffer, "%s", sqlite3_column_text(pStmt, 0));
      strBuffer = cBuffer;
      vecResults.push_back(strBuffer);
    }
    sqlite3_finalize(pStmt);
    printf("Found %d results.n", vecResults.size());
    for (unsigned int i = 0, iEnd = vecResults.size(); i != iEnd; ++i)
    {
      printf("%d: %sn", i, vecResults[i].c_str());
    }
  }
  sqlite3_close(pDB);
  return 0;
}

因为行不是字符串

将第二个参数的绑定更改为sqlite3_bind_int(pStmt, 2, 3);,它应该可以正常工作。

还有,为什么将名称行设置为VARCHAR(30)而不是TEXT?