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. QUERY NOT WORKING
Forum Updated to NodeBB v4.3 + New Features

QUERY NOT WORKING

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 718 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.
  • GREYONG Offline
    GREYONG Offline
    GREYON
    wrote on last edited by
    #1

    Hello people,I have a problem which needs your help.I have a query which should insert data in a 20 colunm table but instead is giving me parameter mismatch error. I have gone through the code i cannot find the error. My app is well connected to the database .I have tried to insert using the sqlite studio command its working. What can be the problem? Below is my code.

     QSqlQuery qry;
    
           qry.prepare("INSERT INTO STAFF(SURNAME,  LASTNAME, GENDER , BIRTH_D, M_STATUS, NRC_NO,"
                       "  EMPLOYEE_NO, TS_NO, DATE_FIRST_APP, DATE_PRESENT_APP, POSITION , SUBJECT_ONE,"
                       " SUBJECT_TWO, DEPARTMENT_ONE, DEPARTMENT_TWO, QUALIFICATIONS , SALARY, CONFIRMED,"
                       " PHONE, E_MAIL)"
    
                         "VALUES( :SURNAME,  :LASTNAME,  :GENDER,   :BIRTH_D,  :M_STATUS, :NRC_NO, :EMPLOYEE_NO, :TS_NO,"
                       " :DATE_FIRST_APP, :DATE_PRESENT_APP, :POSITION, :SUBJECT_ONE, :SUBJECT_TWO, :DEPARTMENT_ONE,"
                       " :DEPARTMENT_TWO, :QUALIFICATIONS, :SALARY, :CONFIRMED, :PHONE, :E_MAIL)");
           
             
           
           qry.bindValue(":SURNAME",surname);
             qry.bindValue(":LASTNAME",lastname);
             qry.bindValue(":GENDER",sex);
             qry.bindValue(":BIRTH_D",dateofbirth);
             qry.bindValue(":M_STATUS",STATUS);
             qry.bindValue(":NRC_NO",NRC);
             qry.bindValue(":EMPLOYEE_NO",EMPLOYEE_NO);
             qry.bindValue(":TS_NO",TS_NO);
             qry.bindValue(":DATE_FIRST_APP",DATE_FIRST);
             qry.bindValue(":DATE_PRESENT_APP",DATE_SECOND);
             qry.bindValue(":POSITION",POSITION);
             qry.bindValue(":SUBJECT_ONE",SUB1);
             qry.bindValue(":SUBJECT_TWO",SUB2);
             qry.bindValue(":DEPARTMENT_ONE",DPT1);
             qry.bindValue(":DEPARTMENT_TWO",DPT2);
             qry.bindValue(":QUALIFICATIONS",QUALIFICATION);
             qry.bindValue(":SALARY",SALARY);
             qry.bindValue(":CONFIRMED",CONFIRMED);
             qry.bindValue(":PHONE",Phone);
             qry.bindValue(":E_MAIL",E_MAIL);
            qry.exec();
            qDebug()<<qry.lastError();
    

    These are error messages

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

    Here is the table
    577a49a3-d9c8-4ab4-a50e-44e135077b09-image.png

    985495cd-6136-431e-a803-fb50b16841bf-image.png

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

      Hi,

      There might be some space missing in your query string.

      You should also surround your query.exec call with an if or you will always print something.

      You should also print the output of lastQuery.

      On a side note, please don't write title in all caps. In written language it's considered shouting and rude.

      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
      1
      • GREYONG Offline
        GREYONG Offline
        GREYON
        wrote on last edited by
        #3

        Okay for responding.but I don't understand when you say the maybe some space missing in the query.And where exactly can an If be there?

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

          The way you built your query string, "VALUES" is likely directly after the ")".

          If (!query.exec()) {
              // put here the code that should run in case of failure
          }
          

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

          GREYONG 1 Reply Last reply
          0
          • SGaistS SGaist

            The way you built your query string, "VALUES" is likely directly after the ")".

            If (!query.exec()) {
                // put here the code that should run in case of failure
            }
            
            GREYONG Offline
            GREYONG Offline
            GREYON
            wrote on last edited by
            #5

            @SGaist okay I get your point,However,the code is there it's only that I didn't include it when sending .it comes just after the **lastError ** line of code in my main code.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mchinand
              wrote on last edited by
              #6

              Check the output of QSqlQuery::boundValues() (after the binding calls), QSqlQuery::lastQuery() and/or QSqlQuery::executedQuery() to see if anything looks different from what you expect.

              GREYONG 1 Reply Last reply
              1
              • M mchinand

                Check the output of QSqlQuery::boundValues() (after the binding calls), QSqlQuery::lastQuery() and/or QSqlQuery::executedQuery() to see if anything looks different from what you expect.

                GREYONG Offline
                GREYONG Offline
                GREYON
                wrote on last edited by
                #7

                @mchinand Thanks let me try that way

                JonBJ 1 Reply Last reply
                0
                • GREYONG GREYON

                  @mchinand Thanks let me try that way

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @GREYON
                  @SGaist suggested you put a space into your )VALUES, did you at least try that?

                  Otherwise: reduce your statement down till it works. Try it with only the SURNAME/:SURNAME column in it. Yes, the INSERT will error before inserting because of a lack of columns, but you are only interested in trying to get as far as that, if you do not get the Parameter count mismatch error you have succeeded. Then add the other columns back in.

                  GREYONG 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @GREYON
                    @SGaist suggested you put a space into your )VALUES, did you at least try that?

                    Otherwise: reduce your statement down till it works. Try it with only the SURNAME/:SURNAME column in it. Yes, the INSERT will error before inserting because of a lack of columns, but you are only interested in trying to get as far as that, if you do not get the Parameter count mismatch error you have succeeded. Then add the other columns back in.

                    GREYONG Offline
                    GREYONG Offline
                    GREYON
                    wrote on last edited by
                    #9

                    @JonB Thanks, your idea of going through it one by one has worked thanks for your help

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      So what was the actual error in your code above then?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      GREYONG 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        So what was the actual error in your code above then?

                        GREYONG Offline
                        GREYONG Offline
                        GREYON
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher it's like one of the column name was not correctly spelled though I couldn't pin point it exactly cause I was just going through them one by one until all of them worked.

                        1 Reply Last reply
                        0

                        • Login

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