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. SQLite and QSqlQuery "Parameter count mismatch" Error

SQLite and QSqlQuery "Parameter count mismatch" Error

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 2.7k 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.
  • P Offline
    P Offline
    ppitu
    wrote on 17 Dec 2020, 18:45 last edited by ppitu
    #1

    Hi
    I have a function to update record in database:

    void SummaryPaymentDao::updateSummaryPayment(const SummaryPayment &summaryPayment) const
    {
        qDebug() << summaryPayment.getProductName();
        QSqlQuery query(mDatabase);
        query.prepare("UPDATE summary_payment SET (product_name = :product_name, price = :price) WHERE id = (:id)");
        query.bindValue(":product_name", summaryPayment.getProductName());
        query.bindValue(":price", summaryPayment.getPrice());
        query.bindValue(":id", summaryPayment.getId());
        query.exec();
        qDebug() << query.lastQuery();
        DatabaseManager::debugQuery(query);
    }
    

    But when i call this function i get error:
    "Parameter count mismatch"
    I tried to look for answers on the forum but no solution worked.

    J 1 Reply Last reply 17 Dec 2020, 19:28
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Dec 2020, 19:02 last edited by
      #2

      What Qt version do you use?
      Please check the return values of query.prepare() and bindValue(). Also re-write your query to UPATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id

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

      1 Reply Last reply
      2
      • P Offline
        P Offline
        ppitu
        wrote on 17 Dec 2020, 19:27 last edited by
        #3

        I am using Qt 1.15.1
        I re-write this line but i have still this problem.
        And i check the lastError():
        Parameter count mismatch"
        And lastQuery():
        UPDATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id

        J 1 Reply Last reply 17 Dec 2020, 19:30
        0
        • P ppitu
          17 Dec 2020, 18:45

          Hi
          I have a function to update record in database:

          void SummaryPaymentDao::updateSummaryPayment(const SummaryPayment &summaryPayment) const
          {
              qDebug() << summaryPayment.getProductName();
              QSqlQuery query(mDatabase);
              query.prepare("UPDATE summary_payment SET (product_name = :product_name, price = :price) WHERE id = (:id)");
              query.bindValue(":product_name", summaryPayment.getProductName());
              query.bindValue(":price", summaryPayment.getPrice());
              query.bindValue(":id", summaryPayment.getId());
              query.exec();
              qDebug() << query.lastQuery();
              DatabaseManager::debugQuery(query);
          }
          

          But when i call this function i get error:
          "Parameter count mismatch"
          I tried to look for answers on the forum but no solution worked.

          J Offline
          J Offline
          JonB
          wrote on 17 Dec 2020, 19:28 last edited by
          #4

          @ppitu
          In addition to @Christian-Ehrlicher 's correction to your syntax.

          SQLite apparently returns "Parameter count mismatch" for "almost any" error :)

          P 1 Reply Last reply 17 Dec 2020, 19:39
          0
          • P ppitu
            17 Dec 2020, 19:27

            I am using Qt 1.15.1
            I re-write this line but i have still this problem.
            And i check the lastError():
            Parameter count mismatch"
            And lastQuery():
            UPDATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id

            J Offline
            J Offline
            JonB
            wrote on 17 Dec 2020, 19:30 last edited by JonB
            #5

            @ppitu
            What did you do about @Christian-Ehrlicher's

            Please check the return values of query.prepare() and bindValue().

            ?

            We do not know what is in your database. We don't know whether you have a table named summary_payment. We don't know whether it has the column names you specify. We don't know whether your code is supplying good values for these.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 17 Dec 2020, 19:33 last edited by Christian Ehrlicher
              #6

              This works fine for me:

              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              db.open();
              QSqlQuery query(db);
              query.exec("CREATE TABLE summary_payment (product_name text, price real, id primary key)");
              query.prepare("UPDATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id");
              query.bindValue(":product_name", QLatin1String("product_name"));
              query.bindValue(":price", 8);
              query.bindValue(":id", 7);
              if (!query.exec()) {
                qDebug() << query.lastError().databaseText() << query.lastError().driverText();
              }
              

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

              1 Reply Last reply
              1
              • J JonB
                17 Dec 2020, 19:28

                @ppitu
                In addition to @Christian-Ehrlicher 's correction to your syntax.

                SQLite apparently returns "Parameter count mismatch" for "almost any" error :)

                P Offline
                P Offline
                ppitu
                wrote on 17 Dec 2020, 19:39 last edited by
                #7

                https://github.com/ppitu/SummaryPaymentApp/blob/main/summary-payment-app/summary-payment-app-core/SummaryPaymentDao.cpp

                Here is a link to the whole file.
                I create the table the same as @Christian-Ehrlicher , and insert to database work correct i have a problem with update and remove

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 17 Dec 2020, 19:41 last edited by
                  #8

                  Did you try my code? If so adjust yours until it's working as expected.

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

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    ppitu
                    wrote on 17 Dec 2020, 19:53 last edited by
                    #9

                    @Christian-Ehrlicher i tried your code (in new project) and it worked fine.
                    But when i change code in my app I get this error: "" "Parameter count mismatch"

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 17 Dec 2020, 19:57 last edited by
                      #10

                      Then simplify your code until it works or looks like mine to find your problem.

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

                      1 Reply Last reply
                      2
                      • P Offline
                        P Offline
                        ppitu
                        wrote on 17 Dec 2020, 20:25 last edited by
                        #11

                        Thanks for help, i found my mistake (in database in table summay_payment i have: summarypayment_id not id)

                        J 1 Reply Last reply 18 Dec 2020, 10:07
                        1
                        • P ppitu
                          17 Dec 2020, 20:25

                          Thanks for help, i found my mistake (in database in table summay_payment i have: summarypayment_id not id)

                          J Offline
                          J Offline
                          JonB
                          wrote on 18 Dec 2020, 10:07 last edited by JonB
                          #12

                          @ppitu
                          Which is why, as I suggested, we could not help you.....

                          You really ought take the time to find one of the various free off-line or on-line SQLite workbench tools available to test your queries outside of Qt while you develop them.

                          1 Reply Last reply
                          0

                          7/12

                          17 Dec 2020, 19:39

                          • Login

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