参数化查询中的参数计数不匹配

发布于 2024-12-05 20:30:41 字数 1512 浏览 1 评论 0原文

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

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

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

I am using quite a lot of parameterized queries in my code for performance reasons. In short, some of them work, some don't.

I initialize the query during construction of my database wrapper like this:

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 is a QSqlQuery member variable of my database wrapper. Later on, in the function that wants to use this query, I do something like

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...
}

I always get the same error message/output:

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*'

The exception is not surprising, but the parameter count mismatch is. The call to boundValues actually shows the right values and all, still I get this error message. I have similar queries that work just fine.

I tried substituting positional bind values, renamed the placeholders, used ? and positional bind values, all to no avail. Does anyone have an idea what the problem might be?

I use Qt 4.7.3 and SQLite 3.7.4-2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-12-12 20:30:41

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

Usually this error means that the SELECT/UPDATE query itself is incorrect. You did not give the schema of the database so it's not possible to pinpoint which one. So one or more of somevalue, sometable, one_feature, or second_feature is not in the database/table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文