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. QSqlQuery "Parameter count mismatch" when binding with name
Qt 6.11 is out! See what's new in the release blog

QSqlQuery "Parameter count mismatch" when binding with name

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 699 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
    masa4
    wrote on last edited by masa4
    #1

    I am using SQLite and it supports named placeholders. I am currently using named placeholders but in some situation they dont work.
    My query that I got error:

    bool DB::myFunc(const QString &col1txt, const QString &col2txt, const QString &col3txt, const QString &col4txt, const int col5int, const QString &col6txt)
    {
    	query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(:col1txt, :col2txt, :col3txt, :col4txt, :col5int, :col6txt)");
    	query.bindValue(":col1txt", col1txt);
    	query.bindValue(":col2txt", col2txt);
    	query.bindValue(":col3txt", col3txt);
    	query.bindValue(":col4txt", col4txt);
    	query.bindValue(":col5int", col5int);
    	query.bindValue(":col6txt", col6txt);
    
        if(query.exec()){
            return true;
        }
        else{
            qDebug() << query.lastError().text();
            return false;
        }
    }
    

    What's wrong with it? And anyone have information if there is such situation named placeholers will not work?

    JonBJ 1 Reply Last reply
    0
    • M masa4

      @JonB No actually i just paste my prepared statement, not all code snippet. i will modify it now.

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

      @masa4
      Change (temporarily) to use void QSqlQuery::bindValue(int pos, const QVariant &val, QSql::ParamType paramType = QSql::In) or void QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) and ? placeholders. Does that make any difference?

      Does mytable indeed require just these 6 parameters? It might be that you get a "Parameter count mismatch" if you do not have the right number/names of columns, I don't know.

      Try statement like

      query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(\"col1txt\", \"col2txt\", \"col3txt\", \"col4txt\", 5, \"col6txt\")");
      

      so no values to bind, does that work?

      M 1 Reply Last reply
      0
      • M masa4

        I am using SQLite and it supports named placeholders. I am currently using named placeholders but in some situation they dont work.
        My query that I got error:

        bool DB::myFunc(const QString &col1txt, const QString &col2txt, const QString &col3txt, const QString &col4txt, const int col5int, const QString &col6txt)
        {
        	query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(:col1txt, :col2txt, :col3txt, :col4txt, :col5int, :col6txt)");
        	query.bindValue(":col1txt", col1txt);
        	query.bindValue(":col2txt", col2txt);
        	query.bindValue(":col3txt", col3txt);
        	query.bindValue(":col4txt", col4txt);
        	query.bindValue(":col5int", col5int);
        	query.bindValue(":col6txt", col6txt);
        
            if(query.exec()){
                return true;
            }
            else{
                qDebug() << query.lastError().text();
                return false;
            }
        }
        

        What's wrong with it? And anyone have information if there is such situation named placeholers will not work?

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

        @masa4
        So far I see 6 placeholders to be bound but only 1 named value actually bound. So I would expect a "Parameter count mismatch". Though with no exec() I don't see where/when you would get the error your report....

        M 1 Reply Last reply
        1
        • JonBJ JonB

          @masa4
          So far I see 6 placeholders to be bound but only 1 named value actually bound. So I would expect a "Parameter count mismatch". Though with no exec() I don't see where/when you would get the error your report....

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

          @JonB No actually i just paste my prepared statement, not all code snippet. i will modify it now.

          JonBJ 1 Reply Last reply
          0
          • M masa4

            @JonB No actually i just paste my prepared statement, not all code snippet. i will modify it now.

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

            @masa4
            Change (temporarily) to use void QSqlQuery::bindValue(int pos, const QVariant &val, QSql::ParamType paramType = QSql::In) or void QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) and ? placeholders. Does that make any difference?

            Does mytable indeed require just these 6 parameters? It might be that you get a "Parameter count mismatch" if you do not have the right number/names of columns, I don't know.

            Try statement like

            query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(\"col1txt\", \"col2txt\", \"col3txt\", \"col4txt\", 5, \"col6txt\")");
            

            so no values to bind, does that work?

            M 1 Reply Last reply
            0
            • JonBJ JonB

              @masa4
              Change (temporarily) to use void QSqlQuery::bindValue(int pos, const QVariant &val, QSql::ParamType paramType = QSql::In) or void QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) and ? placeholders. Does that make any difference?

              Does mytable indeed require just these 6 parameters? It might be that you get a "Parameter count mismatch" if you do not have the right number/names of columns, I don't know.

              Try statement like

              query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(\"col1txt\", \"col2txt\", \"col3txt\", \"col4txt\", 5, \"col6txt\")");
              

              so no values to bind, does that work?

              M Offline
              M Offline
              masa4
              wrote on last edited by
              #5

              @JonB hmm It still throws same error and when i checked db yes you are right there are just 5 columns. There were 6 actually but i probably merged date and time columns as one column afterward. Thanks for help

              JonBJ 1 Reply Last reply
              0
              • M masa4

                @JonB hmm It still throws same error and when i checked db yes you are right there are just 5 columns. There were 6 actually but i probably merged date and time columns as one column afterward. Thanks for help

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

                @masa4
                :) Use my update to last post while you get it right, then change to bound values.

                1 Reply Last reply
                1
                • M masa4 has marked this topic as solved on

                • Login

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