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. Parameter Count Mismatch
Forum Updated to NodeBB v4.3 + New Features

Parameter Count Mismatch

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 1.8k 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.
  • A Offline
    A Offline
    Aromakc
    wrote on 4 Aug 2020, 05:23 last edited by
    #1

    I tried to insert new datas in database and it shows parameter count mismatch. It was similar to the code that I had done recently but didn't worked.

    void CreateAccount::on_pushButton_create_clicked()
    {

        QString name,address,password,username,gender;
        qint64 phone;
        username=ui->line_username->text();
        password=ui->line_password->text();
        name=ui->line_name->text();
        address=ui->line_address->text();
        phone=ui->line_phone->text().toInt();
        if(ui->radioButton_M->isChecked())
        { gender="M";}
        if(ui->radioButton_F->isChecked())
        { gender="F";}
    
    
    
        QSqlQuery query;
    

    //query.prepare("INSERT into Users (username,password,Name,Gender,Address,Phone_Number) values ('"+username+"','"+password+"','"+name+"','"+gender+"','"+address+"',"+phone+")"); (tried this too)

        query.prepare("INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(?, ?, ?, ?, ? ,?)");
    
        query.addBindValue(username);
        query.addBindValue(password);
        query.addBindValue(name);
        query.addBindValue(gender);
        query.addBindValue(address);
        query.addBindValue(phone);
    
    
        if(query.exec()){
             QMessageBox::information(this,tr("Save"),tr("Saved"));}
        else
        {
            QMessageBox::critical(this,tr("Error."),query.lastError().text());
        }
    

    }

    J 1 Reply Last reply 4 Aug 2020, 05:26
    0
    • A Aromakc
      4 Aug 2020, 05:23

      I tried to insert new datas in database and it shows parameter count mismatch. It was similar to the code that I had done recently but didn't worked.

      void CreateAccount::on_pushButton_create_clicked()
      {

          QString name,address,password,username,gender;
          qint64 phone;
          username=ui->line_username->text();
          password=ui->line_password->text();
          name=ui->line_name->text();
          address=ui->line_address->text();
          phone=ui->line_phone->text().toInt();
          if(ui->radioButton_M->isChecked())
          { gender="M";}
          if(ui->radioButton_F->isChecked())
          { gender="F";}
      
      
      
          QSqlQuery query;
      

      //query.prepare("INSERT into Users (username,password,Name,Gender,Address,Phone_Number) values ('"+username+"','"+password+"','"+name+"','"+gender+"','"+address+"',"+phone+")"); (tried this too)

          query.prepare("INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(?, ?, ?, ?, ? ,?)");
      
          query.addBindValue(username);
          query.addBindValue(password);
          query.addBindValue(name);
          query.addBindValue(gender);
          query.addBindValue(address);
          query.addBindValue(phone);
      
      
          if(query.exec()){
               QMessageBox::information(this,tr("Save"),tr("Saved"));}
          else
          {
              QMessageBox::critical(this,tr("Error."),query.lastError().text());
          }
      

      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Aug 2020, 05:26 last edited by jsulm 8 Apr 2020, 05:28
      #2

      @Aromakc Print out https://doc.qt.io/qt-5/qsqlquery.html#executedQuery after executing the query to see how the actual query looks like.
      Also, you use different casing in the column list: is this a mistake?
      (username,password,Name,Gender,Address,Phone_Number)

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

      A 2 Replies Last reply 4 Aug 2020, 06:17
      2
      • J jsulm
        4 Aug 2020, 05:26

        @Aromakc Print out https://doc.qt.io/qt-5/qsqlquery.html#executedQuery after executing the query to see how the actual query looks like.
        Also, you use different casing in the column list: is this a mistake?
        (username,password,Name,Gender,Address,Phone_Number)

        A Offline
        A Offline
        Aromakc
        wrote on 4 Aug 2020, 06:17 last edited by
        #3

        @jsulm (username,password,Name,Gender,Address,Phone_Number) is the name of my columns that I have creaed all being string except Phone_Number
        While I add qDebug()<< query.lastQuery(); before if(que
        r.exec()) it shows:
        "INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(?,?,?,?,?,?)"

        J 1 Reply Last reply 4 Aug 2020, 06:23
        0
        • A Aromakc
          4 Aug 2020, 06:17

          @jsulm (username,password,Name,Gender,Address,Phone_Number) is the name of my columns that I have creaed all being string except Phone_Number
          While I add qDebug()<< query.lastQuery(); before if(que
          r.exec()) it shows:
          "INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(?,?,?,?,?,?)"

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 4 Aug 2020, 06:23 last edited by
          #4

          @Aromakc said in Parameter Count Mismatch:

          before if

          AFTER executing the query...

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

          A 1 Reply Last reply 4 Aug 2020, 06:58
          2
          • J jsulm
            4 Aug 2020, 06:23

            @Aromakc said in Parameter Count Mismatch:

            before if

            AFTER executing the query...

            A Offline
            A Offline
            Aromakc
            wrote on 4 Aug 2020, 06:58 last edited by
            #5

            @jsulmScreenshot (43).png

            K 1 Reply Last reply 4 Aug 2020, 07:25
            0
            • A Aromakc
              4 Aug 2020, 06:58

              @jsulmScreenshot (43).png

              K Offline
              K Offline
              KroMignon
              wrote on 4 Aug 2020, 07:25 last edited by
              #6

              @Aromakc Maybe a silly question, but where did you define the database connection settings?

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              A 1 Reply Last reply 4 Aug 2020, 07:43
              0
              • K KroMignon
                4 Aug 2020, 07:25

                @Aromakc Maybe a silly question, but where did you define the database connection settings?

                A Offline
                A Offline
                Aromakc
                wrote on 4 Aug 2020, 07:43 last edited by
                #7

                @KroMignon I have created a database connection login.h

                QSqlDatabase mydb; // default connection
                void connClose(){
                mydb.close();
                mydb.removeDatabase(QSqlDatabase::defaultConnection); //closing db and removing any connection
                }
                bool connOpen()
                {
                QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
                mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");
                if(!mydb.open()){
                qDebug()<<("Failed to locate database!");
                return false;
                }
                else {
                qDebug()<<("Connected..."); // qDebug("Connected");
                return true;
                }
                }
                i have liked login.h in another createaccount.h and coded
                Login conn;

                I think there is no problem creating two tables in databasename.db

                K J JonBJ 3 Replies Last reply 4 Aug 2020, 07:55
                0
                • A Aromakc
                  4 Aug 2020, 07:43

                  @KroMignon I have created a database connection login.h

                  QSqlDatabase mydb; // default connection
                  void connClose(){
                  mydb.close();
                  mydb.removeDatabase(QSqlDatabase::defaultConnection); //closing db and removing any connection
                  }
                  bool connOpen()
                  {
                  QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
                  mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");
                  if(!mydb.open()){
                  qDebug()<<("Failed to locate database!");
                  return false;
                  }
                  else {
                  qDebug()<<("Connected..."); // qDebug("Connected");
                  return true;
                  }
                  }
                  i have liked login.h in another createaccount.h and coded
                  Login conn;

                  I think there is no problem creating two tables in databasename.db

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 4 Aug 2020, 07:55 last edited by
                  #8

                  @Aromakc Please logout QSqlQuery::executedQuery() which is the real SQL statement which has been executed.

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  2
                  • A Aromakc
                    4 Aug 2020, 07:43

                    @KroMignon I have created a database connection login.h

                    QSqlDatabase mydb; // default connection
                    void connClose(){
                    mydb.close();
                    mydb.removeDatabase(QSqlDatabase::defaultConnection); //closing db and removing any connection
                    }
                    bool connOpen()
                    {
                    QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
                    mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");
                    if(!mydb.open()){
                    qDebug()<<("Failed to locate database!");
                    return false;
                    }
                    else {
                    qDebug()<<("Connected..."); // qDebug("Connected");
                    return true;
                    }
                    }
                    i have liked login.h in another createaccount.h and coded
                    Login conn;

                    I think there is no problem creating two tables in databasename.db

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 4 Aug 2020, 08:13 last edited by
                    #9

                    @Aromakc I was actually talking about https://doc.qt.io/qt-5/qsqlquery.html#executedQuery NOT lastQuery...

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

                    1 Reply Last reply
                    1
                    • A Aromakc
                      4 Aug 2020, 07:43

                      @KroMignon I have created a database connection login.h

                      QSqlDatabase mydb; // default connection
                      void connClose(){
                      mydb.close();
                      mydb.removeDatabase(QSqlDatabase::defaultConnection); //closing db and removing any connection
                      }
                      bool connOpen()
                      {
                      QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
                      mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");
                      if(!mydb.open()){
                      qDebug()<<("Failed to locate database!");
                      return false;
                      }
                      else {
                      qDebug()<<("Connected..."); // qDebug("Connected");
                      return true;
                      }
                      }
                      i have liked login.h in another createaccount.h and coded
                      Login conn;

                      I think there is no problem creating two tables in databasename.db

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 4 Aug 2020, 08:54 last edited by JonB 8 Apr 2020, 09:23
                      #10

                      @Aromakc said in Parameter Count Mismatch:
                      These may not be relevant to your current issue, but:

                      mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db");

                      As pasted here, this is not at all right! I assume you have \\ in your actual code, but it shows as \ here since you did not put it inside code tags when pasting?

                      qint64 phone;
                      phone=ui->line_phone->text().toInt();
                      all being string except Phone_Number

                      This is a bad idea. A phone number is a string of digit characters, not a number/integer.

                      it shows parameter count mismatch

                      You should copy & paste the exact error message you receive, in case that gives us a clue.

                      How do we know your Users table has exactly 6 columns?

                      Also, looking at your screenshot, what are those "duplicate connection name" warnings about? Your code should be running without any such warnings.

                      1 Reply Last reply
                      3
                      • J jsulm
                        4 Aug 2020, 05:26

                        @Aromakc Print out https://doc.qt.io/qt-5/qsqlquery.html#executedQuery after executing the query to see how the actual query looks like.
                        Also, you use different casing in the column list: is this a mistake?
                        (username,password,Name,Gender,Address,Phone_Number)

                        A Offline
                        A Offline
                        Aromakc
                        wrote on 4 Aug 2020, 09:56 last edited by
                        #11

                        @jsulm
                        I coded qDebug()<<query.executedQuery(); it shows
                        "INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(:a,:bb,:bc,:bd,:be,:bf)"

                        J.HilkJ 1 Reply Last reply 4 Aug 2020, 10:21
                        0
                        • A Aromakc
                          4 Aug 2020, 09:56

                          @jsulm
                          I coded qDebug()<<query.executedQuery(); it shows
                          "INSERT into Users (username,password,Name,Gender,Address,Phone_Number) VALUES(:a,:bb,:bc,:bd,:be,:bf)"

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on 4 Aug 2020, 10:21 last edited by
                          #12

                          @Aromakc
                          Ok, try to clean up your query, you're mixing lowercase and capital keywords and don't terminate with a ;

                          query.prepare("INSERT INTO Users (username, password, Name, Gender, Address, Phone_Number) VALUES(?, ?, ?, ?, ? ,?);");


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          1
                          • JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 4 Aug 2020, 10:36 last edited by
                            #13

                            I have posted above questions like "How do we know your Users table has exactly 6 columns?`, but no response.

                            So my last comment is I would try

                            SELECT  username, password, Name, Gender, Address, Phone_Number from Users
                            

                            Up to @Aromakc whether he bothers...

                            A JonBJ 2 Replies Last reply 4 Aug 2020, 11:22
                            1
                            • JonBJ JonB
                              4 Aug 2020, 10:36

                              I have posted above questions like "How do we know your Users table has exactly 6 columns?`, but no response.

                              So my last comment is I would try

                              SELECT  username, password, Name, Gender, Address, Phone_Number from Users
                              

                              Up to @Aromakc whether he bothers...

                              A Offline
                              A Offline
                              Aromakc
                              wrote on 4 Aug 2020, 11:22 last edited by Aromakc 8 Apr 2020, 11:28
                              #14

                              @JonB Here is screenshot of my db and i have changed phone to string to see if it is the problem. Did you mean this or sth else?
                              I fixed those duplicate connection name. It was left there as I was testing my database connectivity.
                              Screenshot (48).png

                              K 1 Reply Last reply 4 Aug 2020, 12:34
                              0
                              • JonBJ JonB
                                4 Aug 2020, 10:36

                                I have posted above questions like "How do we know your Users table has exactly 6 columns?`, but no response.

                                So my last comment is I would try

                                SELECT  username, password, Name, Gender, Address, Phone_Number from Users
                                

                                Up to @Aromakc whether he bothers...

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 4 Aug 2020, 11:55 last edited by JonB 8 Apr 2020, 11:55
                                #15

                                @Aromakc
                                I never suggested the phone number type would be the cause of your issue. But I am pleased you have altered its type off numeric.

                                @JonB said in Parameter Count Mismatch:

                                SELECT username, password, Name, Gender, Address, Phone_Number from Users

                                I suggested you try the above from your Qt program (query.exec()). This may help us identify where your INSERT problem comes from.

                                A 1 Reply Last reply 4 Aug 2020, 16:46
                                0
                                • A Aromakc
                                  4 Aug 2020, 11:22

                                  @JonB Here is screenshot of my db and i have changed phone to string to see if it is the problem. Did you mean this or sth else?
                                  I fixed those duplicate connection name. It was left there as I was testing my database connectivity.
                                  Screenshot (48).png

                                  K Offline
                                  K Offline
                                  KroMignon
                                  wrote on 4 Aug 2020, 12:34 last edited by
                                  #16

                                  @Aromakc As @JonB already asked you: have you fixed the sqlite db filepath?

                                  mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db"); // Bad escape sequence
                                  mydb.setDatabaseName("D:/Retailhub/Retailhub/items.db"); // correct
                                  mydb.setDatabaseName("D:\\Retailhub\\Retailhub\\items.db"); // also correct
                                  

                                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                  JonBJ A 2 Replies Last reply 4 Aug 2020, 12:40
                                  3
                                  • K KroMignon
                                    4 Aug 2020, 12:34

                                    @Aromakc As @JonB already asked you: have you fixed the sqlite db filepath?

                                    mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db"); // Bad escape sequence
                                    mydb.setDatabaseName("D:/Retailhub/Retailhub/items.db"); // correct
                                    mydb.setDatabaseName("D:\\Retailhub\\Retailhub\\items.db"); // also correct
                                    
                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 4 Aug 2020, 12:40 last edited by
                                    #17

                                    @KroMignon
                                    I don't think this is the OP's situation (else he'd get other errors). I tested, and if you type \\ into a post here without code tagging, it comes out as \ when displayed. Which I'm sure is the situation for his case.

                                    1 Reply Last reply
                                    0
                                    • K KroMignon
                                      4 Aug 2020, 12:34

                                      @Aromakc As @JonB already asked you: have you fixed the sqlite db filepath?

                                      mydb.setDatabaseName("D:\Retailhub\Retailhub\items.db"); // Bad escape sequence
                                      mydb.setDatabaseName("D:/Retailhub/Retailhub/items.db"); // correct
                                      mydb.setDatabaseName("D:\\Retailhub\\Retailhub\\items.db"); // also correct
                                      
                                      A Offline
                                      A Offline
                                      Aromakc
                                      wrote on 4 Aug 2020, 15:58 last edited by Aromakc 8 Apr 2020, 16:00
                                      #18

                                      @KroMignon Yes. Thats not the problem. / and (backslash) both works. I will try to manage connection problem creating new project and update here.

                                      1 Reply Last reply
                                      0
                                      • JonBJ JonB
                                        4 Aug 2020, 11:55

                                        @Aromakc
                                        I never suggested the phone number type would be the cause of your issue. But I am pleased you have altered its type off numeric.

                                        @JonB said in Parameter Count Mismatch:

                                        SELECT username, password, Name, Gender, Address, Phone_Number from Users

                                        I suggested you try the above from your Qt program (query.exec()). This may help us identify where your INSERT problem comes from.

                                        A Offline
                                        A Offline
                                        Aromakc
                                        wrote on 4 Aug 2020, 16:46 last edited by
                                        #19

                                        @JonB
                                        I have created "Users" table after creating "Inventory" table in items.db from Sqlite Studio. Using another database manager software I could see Both Tables in items.db but
                                        if(query.exec(Select username........ from Users)) showed Users is unavailable.
                                        I used sqlite3 from terminal and it showed Error: No such Tables: Users
                                        So I deleted Users from Sqlite Studio and entered new Users table from terminal and it Worked.

                                        (I will look more into Sqlite Studio as changes made into Inventory form the same app worked.)

                                        Thank you and other contributors.

                                        1 Reply Last reply
                                        0

                                        1/19

                                        4 Aug 2020, 05:23

                                        • Login

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