【发布时间】:2011-11-23 11:16:27
【问题描述】:
出于性能原因,我在代码中使用了很多参数化查询。简而言之,其中一些有效,有些无效。
我在构建数据库包装器期间初始化查询,如下所示:
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 的调用实际上显示了正确的值和所有值,但我仍然收到此错误消息。我有类似的查询,效果很好。
我尝试替换位置绑定值,重命名占位符,使用? 和位置绑定值,但均无济于事。有谁知道问题可能是什么?
我使用 Qt 4.7.3 和 SQLite 3.7.4-2
【问题讨论】:
-
表中的字段是否声明为 int ?您可以尝试使用文本(并传递带引号的字符串)吗?
-
是的,这些字段在生成的 SQL 脚本中声明为
"one_feature" integer NOT NULL REFERENCES "feature_table" ("id")。不幸的是,更换桌子不是一种选择。我也尝试在querystring中引用参数,但这也不起作用。 -
其他人的面包屑:我遇到了这个错误,但我的问题是我没有用反引号 ` 引用列名,而且我的列名也是一个 sqlite 关键字。