为了提高性能,我在代码中使用了相当多的参数化查询。总之,其中一些工作,一些不工作。在参数化查询中参数计数不匹配
我建设我的数据库包装的过程中初始化这样的查询:
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?你可以尝试使用文本(并通过引用字符串)? – Louis
是的,这些字段在生成SQL脚本中声明为“one_feature”整数NOT NULL REFERENCES“feature_table”(“id”)'。不幸的是,改变表格不是一种选择。我也尝试引用'querystring'中的参数,但那也不起作用。 – arne
面包屑为他人:我有这个错误,但我的问题是,我没有引用反向列名',我有一个列名也是一个sqlite关键字。 –