关于 c:Qt SQLite 没有查询或参数计数不匹配 | 珊瑚贝

Qt SQLite no query or parameter count mismatch


在收到 2 个不同的错误之间进行权衡。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void summary::on_pushButton_saveSummary_clicked()  
{
    if(db.open())
    {
        query.exec(“insert or replace into [PN:”+partNum+” CN:”+chargeNum+“](total, defects, rust) values(1, 2, 3)”);
        if (!query.exec())
        {
        qDebug() << query.lastError();
        qDebug() << query.exec()<<endl;
        }
     }
    else
    {
        qDebug() << db.lastError();
    }
}

上面给出了错误:QSqlError(“,”Unable to fetch row”,”No query”)

而:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void summary::on_pushButton_saveSummary_clicked()
{
    if(db.open())
    {
        qDebug() <<“db open”;
        int a = 3;
        int b = 1;
        int c = 3;

        query.prepare(“insert into [PN:”+partNum+” CN:”+chargeNum+“](total, defects, rust) values(:total, :defects, :rust)”);
        query.bindValue(“:total”, a);
        query.bindValue(“:defects”, b);
        query.bindValue(“:rust”, c);

        if (!query.exec())
        {
        qDebug() << query.lastError();
        qDebug() << query.exec()<<endl;
        }
    }
    else
    {
        qDebug() << db.lastError();
    }
}

产量:QSqlError(“,”参数计数不匹配”,”)

构造函数有:

1
2
3
4
5
6
db.setDatabaseName(“/home/igraves/Databases/testdb”);
db.open();
QString partNum =“134345”;
QString chargeNum =“3452”;
query.prepare(“create table if not exists [PN:”+partNum+” CN:”+chargeNum+“](total int, defects int, rust int)”);
query.exec();

正在创建表,我可以看到它。所以我猜语法?虽然它是 Qt wiki 写的……

编辑:
添加一些 .h 的东西

1
2
3
4
    QString partNum;
    QString chargeNum;
    QSqlDatabase db = QSqlDatabase::addDatabase(“QSQLITE”);
    QSqlQuery query;

答案:这是我的 QString 变量是临时的,在构造函数完成后,它们超出了范围。改变了我的 .h

1
2
QString partNum =“124124”;
QString chargeNum =“234234”;

我至少看到以下错误:

  • 您不设置数据库路径。
  • 创建表时不要打开数据库。
  • “134345” 和 “3452” 被分配给临时变量而不是类成员

考虑到上述情况,构造函数中必须有以下内容:

1
2
3
4
5
6
7
partNum =“134345”;
chargeNum =“3452”;
db.setDatabaseName(“/path/of/database.db”);
if(db.open())
    query.exec(QString(“create table if not exists [PN:%1 CN:%2](total int, defects int, rust int)”)
               .arg(partNum)
               .arg(chargeNum));
  • 抱歉,那些在里面,但我忘了在代码块中添加它们。


来源:https://www.codenong.com/60310396/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9563.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?