qsqlquery'参数计数不匹配'

发布于 2025-01-25 14:44:58 字数 2048 浏览 2 评论 0原文

我正在尝试从表单上的六个字段中获取数据,并将用户输入数据库(本地SQLITE3数据库)传递数据。 ID是一个整数,主键,其余的是足够长的Varchars。在阅读了许多相关问题之后,我仍然无法弄清楚我的代码在哪里出错。我已经确认该程序已连接到数据库,但是我继续获得“参数计数不匹配”。我要去哪里?我正在使用QT 5.15和C ++。存在表工具,因为我在SQLite Manager中打开了它,并且已经插入了测试行。

以下是应该提交数据的功能:

//passes data to database upon clicking submit button
void Add_item::on_submitButton_clicked()    {
    QString name, location, safety, summary, uses, idNumber;
    idNumber =       ui->testIdBox->text();
    name =           ui->nameField->text();
    location =       ui->locationField->text();
    safety =         ui->safetyField->text();
    summary =        ui->summaryField->text();
    uses =           ui->useField->text();

    QSqlQuery qry;
    qry.prepare("INSERT INTO TOOLS (TOOL_ID, TOOL_NAME, TOOL_SUMMARY, TOOL_LOCATION, TOOL_USE, TOOL_SAFETY) "
                "VALUES (:idNumber, :name, :summary, :location, :uses, :safety)");
    
    //binding all values to prevent sql injection attacks
    qry.bindValue(":idNumber", idNumber);
    qry.bindValue(":name", name);
    qry.bindValue(":summary", summary);
    qry.bindValue(":location", location);
    qry.bindValue(":uses", uses);
    qry.bindValue(":safety", safety);


    if(qry.exec()){
        QMessageBox::critical(this,tr("Confirmation Message"),tr("Success!"));
    }
    else    {
        QMessageBox::critical(this,tr("Confirmation Message"),tr("Error, data was not saved."), qry.lastError().text());
    }
    connClose();
}

我在这里连接到数据库:

//connecting to database
bool Add_item::connOpen()   {
    QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("C:/Users/laesc/OneDrive/Documents/ToolBuddy/test.db");
    if (mydb.open())    {
        ui->statusLabel->setText("Connected!");
        qDebug()<<("Connected");
        return true;
    }
    else    {
        ui->statusLabel->setText("Connection Not Successful...");
        qDebug()<<("Not Connected");
        return false;
    }
}

I am trying to take data from six fields on a form and pass the data the user enters into a database (A local SQLite3 database). The ID is an integer and the primary key and the rest are varchars of enough length. After reading a lot of related questions, I still cannot figure out where my code is going wrong. I have confirmation that the program is connected to the database, but I keep getting 'parameter count mismatch'. Where am I going wrong? I'm using Qt 5.15 and C++. The table TOOL exists, as I have it open in SQLite Manager with a test row already inserted.

Below is the function that is supposed to submit the data:

//passes data to database upon clicking submit button
void Add_item::on_submitButton_clicked()    {
    QString name, location, safety, summary, uses, idNumber;
    idNumber =       ui->testIdBox->text();
    name =           ui->nameField->text();
    location =       ui->locationField->text();
    safety =         ui->safetyField->text();
    summary =        ui->summaryField->text();
    uses =           ui->useField->text();

    QSqlQuery qry;
    qry.prepare("INSERT INTO TOOLS (TOOL_ID, TOOL_NAME, TOOL_SUMMARY, TOOL_LOCATION, TOOL_USE, TOOL_SAFETY) "
                "VALUES (:idNumber, :name, :summary, :location, :uses, :safety)");
    
    //binding all values to prevent sql injection attacks
    qry.bindValue(":idNumber", idNumber);
    qry.bindValue(":name", name);
    qry.bindValue(":summary", summary);
    qry.bindValue(":location", location);
    qry.bindValue(":uses", uses);
    qry.bindValue(":safety", safety);


    if(qry.exec()){
        QMessageBox::critical(this,tr("Confirmation Message"),tr("Success!"));
    }
    else    {
        QMessageBox::critical(this,tr("Confirmation Message"),tr("Error, data was not saved."), qry.lastError().text());
    }
    connClose();
}

Here is where I connect to the database:

//connecting to database
bool Add_item::connOpen()   {
    QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("C:/Users/laesc/OneDrive/Documents/ToolBuddy/test.db");
    if (mydb.open())    {
        ui->statusLabel->setText("Connected!");
        qDebug()<<("Connected");
        return true;
    }
    else    {
        ui->statusLabel->setText("Connection Not Successful...");
        qDebug()<<("Not Connected");
        return false;
    }
}

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

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

发布评论

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

评论(1

小猫一只 2025-02-01 14:44:58

对于未来的观众来说,我最终将参数数减少到2,并且查询工作了。出于某种原因,如果我试图绑定超过2个变量,则查询无效。我只是在做多个查询。

For future viewers, I ended up reducing the number of parameters to 2 and the query worked. For one reason or another the query didn't work if I tried to bind more than 2 variables. I'm just doing multiple queries.

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