Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT6 SQLite and QSqlQuery "Parameter count mismatch" Error

QT6 SQLite and QSqlQuery "Parameter count mismatch" Error

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 569 Views
  • 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.
  • M Offline
    M Offline
    Meskaz285
    wrote on 30 Jun 2022, 13:36 last edited by Meskaz285
    #1

    I wrote this function to add data to a database:

    void MainWindow::save1() {
    QString stid,stname,stsur,stage,stgen,stbday,stcourses;
    stid=ui->Stid->text();
    stname=ui->name->text();
    stsur=ui->surname->text();
    stage=ui->age->text();
    if(ui->male->isChecked())
    stgen="male";
    if(ui->female->isChecked())
    stgen="female";
    stbday=ui->birthday->text();
    stcourses=ui->courses->text();
    QSqlQuery qry;
    qry.prepare("INSERT INTO Student (Student ID,Name,Surname,Age,Gender,Birthday,Courses) VALUES "
    "(:stid,:stname,:stsur,:stage,:stgen,:stbday,:stcourses)");
    qry.bindValue(":stid", stid);
    qry.bindValue(":stname",stname);
    qry.bindValue(":stsur",stsur);
    qry.bindValue(":stage",stage);
    qry.bindValue(":stgen",stgen);
    qry.bindValue(":stbday",stbday);
    qry.bindValue(":stcourses",stcourses);
    qry.exec();
    }

    I keep getting QSqlError("", "Parameter count mismatch", "").
    I tried to find a solution but had no luck. I would like some help here

    J J 2 Replies Last reply 30 Jun 2022, 13:56
    0
    • M Meskaz285
      30 Jun 2022, 13:36

      I wrote this function to add data to a database:

      void MainWindow::save1() {
      QString stid,stname,stsur,stage,stgen,stbday,stcourses;
      stid=ui->Stid->text();
      stname=ui->name->text();
      stsur=ui->surname->text();
      stage=ui->age->text();
      if(ui->male->isChecked())
      stgen="male";
      if(ui->female->isChecked())
      stgen="female";
      stbday=ui->birthday->text();
      stcourses=ui->courses->text();
      QSqlQuery qry;
      qry.prepare("INSERT INTO Student (Student ID,Name,Surname,Age,Gender,Birthday,Courses) VALUES "
      "(:stid,:stname,:stsur,:stage,:stgen,:stbday,:stcourses)");
      qry.bindValue(":stid", stid);
      qry.bindValue(":stname",stname);
      qry.bindValue(":stsur",stsur);
      qry.bindValue(":stage",stage);
      qry.bindValue(":stgen",stgen);
      qry.bindValue(":stbday",stbday);
      qry.bindValue(":stcourses",stcourses);
      qry.exec();
      }

      I keep getting QSqlError("", "Parameter count mismatch", "").
      I tried to find a solution but had no luck. I would like some help here

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 30 Jun 2022, 13:56 last edited by
      #2

      @Meskaz285 said in QT6 SQLite and QSqlQuery "Parameter count mismatch" Error:

      Student ID

      You seem to have a space between Student and ID

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 30 Jun 2022, 13:59
      0
      • J jsulm
        30 Jun 2022, 13:56

        @Meskaz285 said in QT6 SQLite and QSqlQuery "Parameter count mismatch" Error:

        Student ID

        You seem to have a space between Student and ID

        M Offline
        M Offline
        Meskaz285
        wrote on 30 Jun 2022, 13:59 last edited by
        #3

        @jsulm that was how i named it in the db file. Should i not name it that way?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chrisw01
          wrote on 30 Jun 2022, 15:31 last edited by
          #4

          @Meskaz285 You may need to wrap your names with a ` tick. I personally have had a lot of problems with bindValue so I switched to building the string to be executed.

          e.g.:

                   QString sqlString = QString("INSERT INTO Student(`Student ID`, `Name`, `Surname`, `Age`, `Gender`, `Birthday`, `Courses`) VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7')").arg(stid).arg(stname).arg(stsur).arg(stage).arg(stgen).arg(stbday).arg(stcourses);
          
                  success = false; errorCount = 0; // success is a bool and errorCount is a int.
          
                  while((success = sql.exec(sqlString)) == false && (errorCount++ < 10)) {
                      QTime dieTime = QTime::currentTime().addSecs(1);
                      while(QTime::currentTime() < dieTime)
                          QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
                  }
                  if((errorCount > 0) && (success == false)) {
                      // Handle your failure here
                  }
                  else {
                   // handle your success here
                  }
          
          

          I have found this method to work best for me. I always had issues with bindValue and the second part of the code you may not need if you use a local sqlServer. I was having issues when using a remote server with sql.exec failing if the network conditions was not perfect.

          Hope that helps...

          1 Reply Last reply
          0
          • M Meskaz285
            30 Jun 2022, 13:36

            I wrote this function to add data to a database:

            void MainWindow::save1() {
            QString stid,stname,stsur,stage,stgen,stbday,stcourses;
            stid=ui->Stid->text();
            stname=ui->name->text();
            stsur=ui->surname->text();
            stage=ui->age->text();
            if(ui->male->isChecked())
            stgen="male";
            if(ui->female->isChecked())
            stgen="female";
            stbday=ui->birthday->text();
            stcourses=ui->courses->text();
            QSqlQuery qry;
            qry.prepare("INSERT INTO Student (Student ID,Name,Surname,Age,Gender,Birthday,Courses) VALUES "
            "(:stid,:stname,:stsur,:stage,:stgen,:stbday,:stcourses)");
            qry.bindValue(":stid", stid);
            qry.bindValue(":stname",stname);
            qry.bindValue(":stsur",stsur);
            qry.bindValue(":stage",stage);
            qry.bindValue(":stgen",stgen);
            qry.bindValue(":stbday",stbday);
            qry.bindValue(":stcourses",stcourses);
            qry.exec();
            }

            I keep getting QSqlError("", "Parameter count mismatch", "").
            I tried to find a solution but had no luck. I would like some help here

            J Offline
            J Offline
            JonB
            wrote on 30 Jun 2022, 15:49 last edited by JonB
            #5

            @Meskaz285
            Before you decide to follow @Chrisw01's route and abandon bindings in favour of literal inlines (which has its own issues), from where you are now you have only to try:

            qry.prepare("INSERT INTO Student (`Student ID`,Name,Surname,Age,Gender,Birthday,Courses) 
            

            and the rest the same. You will always need to quote a column name which you have chosen to have a space in whenever you use it in SQL statements, e.g. when you get to your SELECT statements. And how you quote varies across SQL implementations. Up to you, but I wouldn't dream of choosing to have a space in a column name for the hassle :) Some people use underscore characters for that.

            M 1 Reply Last reply 30 Jun 2022, 16:20
            1
            • J JonB
              30 Jun 2022, 15:49

              @Meskaz285
              Before you decide to follow @Chrisw01's route and abandon bindings in favour of literal inlines (which has its own issues), from where you are now you have only to try:

              qry.prepare("INSERT INTO Student (`Student ID`,Name,Surname,Age,Gender,Birthday,Courses) 
              

              and the rest the same. You will always need to quote a column name which you have chosen to have a space in whenever you use it in SQL statements, e.g. when you get to your SELECT statements. And how you quote varies across SQL implementations. Up to you, but I wouldn't dream of choosing to have a space in a column name for the hassle :) Some people use underscore characters for that.

              M Offline
              M Offline
              Meskaz285
              wrote on 30 Jun 2022, 16:20 last edited by
              #6

              @JonB Ah thank you. This worked for me

              1 Reply Last reply
              0

              1/6

              30 Jun 2022, 13:36

              • Login

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