Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QSqlError("", "Parameter count mismatch", "")
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 4 Posters 998 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Hasibul Hasan ChowdhuryH Offline
    Hasibul Hasan ChowdhuryH Offline
    Hasibul Hasan Chowdhury
    wrote on last edited by
    #1

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

    JonBJ 1 Reply Last reply
    0
    • Hasibul Hasan ChowdhuryH Hasibul Hasan Chowdhury

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

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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?

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          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 .

          I light my way forward with the fires of all the bridges I've burned behind me.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved