BaseResult::exec: Parameter mismatch



  • Hi!
    An error occurred while editing the data in QSqlRelationalTableModel/QSqlTableModel.

    Error: QIBaseResult::exec: Parameter mismatch, expected 0, got 2 parameters

    Database Firebird 3.0, Qt 5.8, OS Ubuntu
    If use database MySQL, error does not occur, everything works.
    What could be the problem?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show the query you are using ?



  • I do not use the request, I'm using QTableView + QSqlRelationalTableModel

    #include "listform.h"
    #include <QVBoxLayout>
    #include <QAbstractItemView>
    #include <QHeaderView>
    #include <QSqlRelationalDelegate>
    
    ListForm::ListForm(QWidget *parent)
    {
        QVBoxLayout *vl = new QVBoxLayout(parent);
        toolBar = new QToolBar(parent);
        tableView = new QTableView(parent);
        tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
        tableView->setSelectionMode(QAbstractItemView::SingleSelection);
        tableView->resizeColumnsToContents();
        tableView->horizontalHeader()->setStretchLastSection(true);
        tableView->setItemDelegate(new QSqlRelationalDelegate);
    
    
        vl->addWidget(toolBar);
        vl->addWidget(tableView);
    
        this->setLayout(vl);
    
        actionNew = new QAction(toolBar);
        actionNew->setIcon(QIcon(":images/new.png"));
    
        actionEdit = new QAction(toolBar);
        actionEdit->setIcon(QIcon(":images/edit.png"));
    
        actionDelete = new QAction(toolBar);
        actionDelete->setIcon(QIcon(":images/delete.png"));
    
        actionRefresh = new QAction(toolBar);
        actionRefresh->setIcon(QIcon(":images/refresh.png"));
    
        toolBar->addAction(actionNew);
        toolBar->addAction(actionEdit);
        toolBar->addSeparator();
        toolBar->addAction(actionDelete);
        toolBar->addSeparator();
        toolBar->addAction(actionRefresh);
    }
    
    void ListForm::setModel(QSqlRelationalTableModel *model)
    {
        tableView->setModel(model);
    
    }
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE");
        db.setHostName("localhost");
        db.setDatabaseName("/home/vital/WMS/db/wms.fdb");
        db.setUserName("SYSDBA");
        db.setPassword("masterkey");
    
    //    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //    db.setHostName("localhost");
    //    db.setDatabaseName("jTrade");
    //    db.setUserName("root");
    //    db.setPassword("Gladiator#2017");
    
        if (!db.open()) {
            qDebug() << "Cannot open DB!" << db.lastError();
        } else
            qDebug() << "Connect to DB!";
    
    
        QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
        model->setTable("users");
        model->setRelation(model->fieldIndex("id_employee"),QSqlRelation("employees","id","name"));
        model->select();
        model->setEditStrategy(QSqlTableModel::OnRowChange);
        model->setSort(model->fieldIndex("name"),Qt::AscendingOrder);
    
        ListForm *lf = new ListForm();
        lf->setParent(this,Qt::Window);
        lf->show();
        lf->setModel(model);
    }
    

    [Added code tags ~kshegunov]


  • Lifetime Qt Champion

    You should check last error and query from your model. That might give you more clues about what is happening.



  • QT sent query:
    UPDATE users SET "PASSWD"=? WHERE "users"."ID" = ?
    On request it is visible that parameters are not set
    The question is why?


  • Lifetime Qt Champion

    Looks like prepared query with anonymous placeholders.

    When exactly is that one called ?



  • When to we apply changes. In my case, when we move to new row in QTableView, this query sent to server. It turns out, Qt do not fill query parameters. How to solve this problem?


  • Lifetime Qt Champion

    It's not very clear. Do you mean it happens when you add a new row ?



  • model->setEditStrategy(QSqlRelationalTableModel::OnRowChange);


  • After changing the line, we go to another line. Changes are sent to the server Firebird. Query we transmitted to server by QSqlRelationalTableModel, but without filling in the parameters. Parameters must fill in QSqlRelationalTableModel, but it does not work for Firebird. With MySQL, there are no such problems.
    Sorry for bad English



  • I have similar problem:

        query.prepare("INSERT INTO MATERIJALI(NAZIV, STANJE_U_SILOSU, ZADNJA_POTROSNJA) VALUES(:naz, :sil, :pot)");
        query.bindValue(":naz", ui->eNaziv->text());
        query.bindValue(":sil", ui->eStanje->value());
        query.bindValue(":pot", ui->eDatum->dateTime().toString());
    

    return: QIBaseResult::exec: Parameter mismatch, expected 0, got 3 parameters

    It seems that QIBaseDriver don't support named placeholders. But when I try with positional placeholders, the result is the same:

        query.prepare("INSERT INTO MATERIJALI(NAZIV, STANJE_U_SILOSU, ZADNJA_POTROSNJA) VALUES(?, ?, ?)");
        query.bindValue(0, ui->eNaziv->text());
        query.bindValue(1, ui->eStanje->value());
        query.bindValue(2, ui->eDatum->dateTime().toString());
    

    "QIBaseResult::exec: Parameter mismatch, expected 0, got 3 parameters".


  • Moderators



  • @jsulm here is result of executedQuery():
    "INSERT INTO MATERIJALI(NAZIV, STANJE_U_SILOSU, ZADNJA_POTROSNJA) VALUES(?, ?, ?)"


  • Lifetime Qt Champion

    What if you use addBindValue rather than bindValue ?



  • With:

        query.addBindValue(ui->eNaziv->text());
        query.addBindValue(ui->eStanje->value());
        query.addBindValue(ui->eDatum->dateTime().toString());
    

    result is the same: QIBaseResult::exec: Parameter mismatch, expected 0, got 3 parameters


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.