gpt4 book ai didi

c++ - 参数化查询中的参数计数不匹配

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:05 25 4
gpt4 key购买 nike

出于性能原因,我在代码中使用了大量参数化查询。简而言之,其中一些有效,一些无效。

我在构建数据库包装器期间初始化查询,如下所示:

QString querystring = QString("SELECT somevalue FROM sometable "
"WHERE one_feature = :one_feature AND other_feature = :other_feature ");
myquery = QSqlQuery(db);
myquery.setForwardOnly(true);
myquery.prepare(querystring);

myquery 是我的数据库包装器的一个 QSqlQuery 成员变量。稍后,在想要使用这个查询的函数中,我做了类似的事情

int db_wrapper::fetch_some_value (int arg1, int arg2) {
myquery.clear();
myquery.bindValue(":one_feature", arg1);
myquery.bindValue(":other_feature", arg2);

qDebug() << "Bound values: " << myquery.boundValues();

bool OK = myquery.exec();
if (!OK) {
int number = myquery.lastError().number();
qDebug() << "db error " << number;
qDebug() << "db error " << myquery.lastError().text();

#ifdef USE_EXCEPTIONS
throw "Could not fetch some_value!";
#endif
}

// process data...
}

我总是得到相同的错误消息/输出:

Bound values:  QMap((":one_feature", QVariant(int, 1) ) ( ":other_feature" ,  QVariant(int, 1) ) )  
db error -1
db error " Parameter count mismatch"
terminate called after throwing an instance of 'char const*'

异常并不奇怪,但参数计数不匹配是。对 boundValues 的调用实际上显示了正确的值和所有内容,但我仍然收到此错误消息。我有类似的查询,效果很好。

我尝试替换位置绑定(bind)值、重命名占位符、使用 ? 和位置绑定(bind)值,但都无济于事。有谁知道问题出在哪里?

我使用 Qt 4.7.3 和 SQLite 3.7.4-2

最佳答案

通常此错误意味着 SELECT/UPDATE 查询本身不正确。您没有提供数据库的架构,因此无法确定是哪一个。因此 somevaluesometableone_featuresecond_feature 中的一个或多个不在数据库/表中。

关于c++ - 参数化查询中的参数计数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7525996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com