arn*_*rne 2 c++ sqlite qt qtsql
出于性能原因,我在代码中使用了大量参数化查询.简而言之,其中一些是有效的,有些则不然.
我在构建数据库包装器时初始化查询,如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
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...
}
Run Code Online (Sandbox Code Playgroud)
我总是得到相同的错误消息/输出:
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*'
Run Code Online (Sandbox Code Playgroud)
例外并不奇怪,但参数计数不匹配.调用boundValues实际显示正确的值和所有,仍然我收到此错误消息.我有类似的查询,工作得很好.
我尝试替换位置绑定值,重命名占位符,使用?和位置绑定值,都无济于事.有谁知道问题可能是什么?
我使用Qt 4.7.3和SQLite 3.7.4-2
通常此错误意味着SELECT/UPDATE查询本身不正确.您没有给出数据库的模式,因此无法确定哪一个.因此,一个或多个somevalue,sometable,one_feature,或者second_feature是不是在数据库/表.