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. Qt SQL Query 'Parameter count mismatch' Problem
Forum Updated to NodeBB v4.3 + New Features

Qt SQL Query 'Parameter count mismatch' Problem

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.5k 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
    maydin
    wrote on last edited by maydin
    #1

    Hello,

    I am trying to use binding function of SQL Query but I have encountered a problem.

    I execute query like this:

            QSqlQuery query;
            q = QString("insert into records (id, name, surname, info) values (123, 'asd', 'qqq', ':info')");
            query.prepare(q);
            query.bindValue(":info", "KKKKKK");
            qDebug() << query.boundValues();
            if(!query.exec(q)) {
                 qDebug("Failed to query database: %s", qPrintable(query.lastError().text()));
            }
    

    This query actually works, because there is NO value to be bound and info field in my row becomes ":info".

    Then I change ':info' to :info. Now I should be able to use binding feature, but it doesnt work. It gives me this error:

    QMap((":info", QVariant(QString, "KKKKKK")))
    Failed to query database:  Parameter count mismatch
    

    I tried to solve this problem from internet but everyone says something wrong in field names itself etc. Thats why I gave you the code without binding things.

    I am using Qt 5.11.2 on Ubuntu 18.04. DB is SQLite which has default settings.

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

      @maydin said in Qt SQL Query 'Parameter count mismatch' Problem:

      if(!query.exec(q))

      This is not what you want since you're now simply executing the given query. You want

      if (!query.exec())

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

      M 1 Reply Last reply
      5
      • Christian EhrlicherC Christian Ehrlicher

        @maydin said in Qt SQL Query 'Parameter count mismatch' Problem:

        if(!query.exec(q))

        This is not what you want since you're now simply executing the given query. You want

        if (!query.exec())

        M Offline
        M Offline
        maydin
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks. I literally wasted 2 hours to find this out. There are lots of things similar to this and all of these are decreasing my motivation.

        Idk, I think there should be a warning in function exec(q), since if you prepare something, you probably will not use the "bad" function.

        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