出于性能原因,我在代码中使用了很多参数化查询。简而言之,其中一些有效,有些无效。
我在构建数据库包装器期间初始化查询,如下所示:
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