在SQLite3中绑定参数是如何工作的(最小示例)
How does binding parameters work in SQLite3 (with minimal example)?
有人建议在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?
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- 无法在windows上使用mingw将sqlite3与c连接
- C++中的memset函数工作不正常
- SQLite3 第二次无法正常工作
- 在SQLite3中绑定参数是如何工作的(最小示例)