Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSqlError("", "Parameter count mismatch", "")



  • I found this error every time but I dont understand where is the problem. Please help me.
    Here is my code:
    QString name = ui->txt_Name->text();
    QString cont = ui->txt_contact->text();
    int id = ui->txt_id->text().toInt();

    const QString save_stmnt = "insert into contact(ID, NAME, MOBILE ) values(:id, :name, :cont)";
    
    db->getQuery()->prepare(save_stmnt);
    
    db->getQuery()->bindValue(":id",id);
    db->getQuery()->bindValue(":name",name);
    db->getQuery()->bindValue(":cont",cont);
    
    if(db->getQuery()->exec()){
        qDebug() << "saved";
        db->getDatabase().close();
    } else {
        qDebug() << db->getQuery()->lastError();
    
    }
    

    Databse connection:
    #include "db_man.h"
    #include <QSqlError>

    DB_MAN::DB_MAN()
    {
    qDebug() << "Connecting ...";
    }

    void DB_MAN::conDB()
    {
    //create database
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("phonebook.db");
    database.open ();

    if(database.isOpen()){
        qDebug() << "Database connected.";
    } else {
        qDebug() << database.lastError();
    }
    
    //set query db
    query = new QSqlQuery(database);
    

    }

    QSqlQuery *DB_MAN::getQuery()
    {
    return query;
    }

    QSqlDatabase DB_MAN::getDatabase()
    {
    return database;
    }



  • @Hasibul-Hasan-Chowdhury
    I'm not sure. You could try delving deeper into database.lastError() via databaseText() or driverText(). It might be that if you have e.g. more columns than those specified which do not allow NULL or have a DEFAULT, or even if it has fewer columns, you might get that message.

    QSqlQuery::prepare() returns a bool. You should check that before trying to exec().

    If it were me I'd try replacing your INSERT line with something literal to see if the Parameter count mismatch goes away:

    const QString save_stmnt = "insert into contact(ID, NAME, MOBILE ) values(23, 'name', '0555 555 5555')";

    or whatever appropriate, to avoid the bindValue()s. Does that make any difference?


  • Lifetime Qt Champion

    Hi,

    What kind of database is it ?
    What version of Qt are you using ?



  • show the database table definition. You probably have a keyed field (not null) that you are not supplying a default value for, as was mentioned by @JonB .


Log in to reply