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. bindValue is not working

bindValue is not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 1.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.
  • I Offline
    I Offline
    icoicqico
    wrote on last edited by icoicqico
    #1
    bool DbManager::add_value(const QString& col, const auto& value){
            bool success = false;
            QSqlQuery query;  
            query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
            query.bindValue(":ac","001"); 
      
            if (query.exec()){
                success = true;
            }
            else qDebug()<<"INSERT error"<<query.lastError();
            qDebug() << query.executedQuery();
            return success;
        }
    

    debug return shows the value I want to bind is ?, I have tried pass Qstring& variable, still the same, but it works when I just run query.prepare with manually input data and not using bindvalue.
    "INSERT INTO idcard_data (Accountid) Values (?)"

    I have searched many posts that reported to have this issue but couldn't find any legit solution, I hope if someone know what happening can give me some helps, thanks a lot.

    Edit: if I use Qstring& variable like this,

    query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");
            query.bindValue(":A",col);
            query.bindValue(":ac","hello");
    

    it gives me this error:

    INSERT error QSqlError("", "Parameter count mismatch", "")
    "INSERT INTO idcard_data (?) Values (?)"
    
    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • I icoicqico
      bool DbManager::add_value(const QString& col, const auto& value){
              bool success = false;
              QSqlQuery query;  
              query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
              query.bindValue(":ac","001"); 
        
              if (query.exec()){
                  success = true;
              }
              else qDebug()<<"INSERT error"<<query.lastError();
              qDebug() << query.executedQuery();
              return success;
          }
      

      debug return shows the value I want to bind is ?, I have tried pass Qstring& variable, still the same, but it works when I just run query.prepare with manually input data and not using bindvalue.
      "INSERT INTO idcard_data (Accountid) Values (?)"

      I have searched many posts that reported to have this issue but couldn't find any legit solution, I hope if someone know what happening can give me some helps, thanks a lot.

      Edit: if I use Qstring& variable like this,

      query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");
              query.bindValue(":A",col);
              query.bindValue(":ac","hello");
      

      it gives me this error:

      INSERT error QSqlError("", "Parameter count mismatch", "")
      "INSERT INTO idcard_data (?) Values (?)"
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please adjust your post so the code can be read by others - please use the code tags.

      And for your code - please check the return value of query.prepare(). The statement with '?' is correct. What does query.lastError() print?

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

      I 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Please adjust your post so the code can be read by others - please use the code tags.

        And for your code - please check the return value of query.prepare(). The statement with '?' is correct. What does query.lastError() print?

        I Offline
        I Offline
        icoicqico
        wrote on last edited by icoicqico
        #3

        @Christian-Ehrlicher Thanks for the reply, no last error is printed, and I have checked the database no correct value is added. But if I just manually input the data to the statement without bindValue the data is correctly update.

        Christian EhrlicherC 1 Reply Last reply
        0
        • I icoicqico

          @Christian-Ehrlicher Thanks for the reply, no last error is printed, and I have checked the database no correct value is added. But if I just manually input the data to the statement without bindValue the data is correctly update.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So when no error is printed then query.exec() succeeded and all is fine.

          You still not check the return value of query.prepare()

          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
          • I icoicqico
            bool DbManager::add_value(const QString& col, const auto& value){
                    bool success = false;
                    QSqlQuery query;  
                    query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
                    query.bindValue(":ac","001"); 
              
                    if (query.exec()){
                        success = true;
                    }
                    else qDebug()<<"INSERT error"<<query.lastError();
                    qDebug() << query.executedQuery();
                    return success;
                }
            

            debug return shows the value I want to bind is ?, I have tried pass Qstring& variable, still the same, but it works when I just run query.prepare with manually input data and not using bindvalue.
            "INSERT INTO idcard_data (Accountid) Values (?)"

            I have searched many posts that reported to have this issue but couldn't find any legit solution, I hope if someone know what happening can give me some helps, thanks a lot.

            Edit: if I use Qstring& variable like this,

            query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");
                    query.bindValue(":A",col);
                    query.bindValue(":ac","hello");
            

            it gives me this error:

            INSERT error QSqlError("", "Parameter count mismatch", "")
            "INSERT INTO idcard_data (?) Values (?)"
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @icoicqico
            Firstly, as @Christian-Ehrlicher says. Put in error checking.

            query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");

            Don't waste time on this, it is wrong and will not work.

                query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
                query.bindValue(":ac","001");
            

            I just run query.prepare with manually input data

            What type is AccountId in your database? Have you really chosen a (3-character) string as your Accountid? Show your code for this "manually input data" case.

            I 1 Reply Last reply
            0
            • JonBJ JonB

              @icoicqico
              Firstly, as @Christian-Ehrlicher says. Put in error checking.

              query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");

              Don't waste time on this, it is wrong and will not work.

                  query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
                  query.bindValue(":ac","001");
              

              I just run query.prepare with manually input data

              What type is AccountId in your database? Have you really chosen a (3-character) string as your Accountid? Show your code for this "manually input data" case.

              I Offline
              I Offline
              icoicqico
              wrote on last edited by
              #6

              @JonB It is not the type problem, ok let's use 'hello' as an example,

              query.prepare("INSERT INTO idcard_data (Accountid) Values ('hello')");
              

              return:

              "INSERT INTO idcard_data (Accountid) Values ('hello')"
              

              And I check my database the value "hello" is updated.

              But if I do it this way, col is "Accountid"

              query.prepare("INSERT INTO idcard_data (:A) Values ('hello')");
              query.bindValue(":A",col);
              
              INSERT error QSqlError("", "Parameter count mismatch", "")
              "INSERT INTO idcard_data (?) Values ('hello')"
              

              and no update on the databse.

              JonBJ I 2 Replies Last reply
              0
              • I icoicqico

                @JonB It is not the type problem, ok let's use 'hello' as an example,

                query.prepare("INSERT INTO idcard_data (Accountid) Values ('hello')");
                

                return:

                "INSERT INTO idcard_data (Accountid) Values ('hello')"
                

                And I check my database the value "hello" is updated.

                But if I do it this way, col is "Accountid"

                query.prepare("INSERT INTO idcard_data (:A) Values ('hello')");
                query.bindValue(":A",col);
                
                INSERT error QSqlError("", "Parameter count mismatch", "")
                "INSERT INTO idcard_data (?) Values ('hello')"
                

                and no update on the databse.

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

                @icoicqico
                You reply to me, where I just wrote:

                query.prepare("INSERT INTO idcard_data (:A) Values (:ac)");

                Don't waste time on this, it is wrong and will not work.

                There is no point you doing this again and show the error it produces given that this is not allowed. Did you read that I wrote this?

                Both @Christian-Ehrlicher and I have now said repeatedly to check the result from any prepare() statements. If you want help why do you simply ignore this? It's pretty frustrating for responders to ask you questions or tell you what to do/not do if you're not going to act on these.

                The only one we are interested in at present is your original:

                        query.prepare("INSERT INTO idcard_data (Accountid) Values (:ac)");
                        query.bindValue(":ac","001"); 
                
                1 Reply Last reply
                0
                • I icoicqico

                  @JonB It is not the type problem, ok let's use 'hello' as an example,

                  query.prepare("INSERT INTO idcard_data (Accountid) Values ('hello')");
                  

                  return:

                  "INSERT INTO idcard_data (Accountid) Values ('hello')"
                  

                  And I check my database the value "hello" is updated.

                  But if I do it this way, col is "Accountid"

                  query.prepare("INSERT INTO idcard_data (:A) Values ('hello')");
                  query.bindValue(":A",col);
                  
                  INSERT error QSqlError("", "Parameter count mismatch", "")
                  "INSERT INTO idcard_data (?) Values ('hello')"
                  

                  and no update on the databse.

                  I Offline
                  I Offline
                  icoicqico
                  wrote on last edited by
                  #8

                  @icoicqico said in bindValue is not working:

                  "INSERT INTO idcard_data (?) Values ('hello')"

                  This is the result of query.executedQuery(), or you want something else?

                  Christian EhrlicherC JonBJ 2 Replies Last reply
                  0
                  • I icoicqico

                    @icoicqico said in bindValue is not working:

                    "INSERT INTO idcard_data (?) Values ('hello')"

                    This is the result of query.executedQuery(), or you want something else?

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    A column can not be bound, only values, I'm pretty sure query.exec() returns false but you ignore me.

                    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
                    • M Offline
                      M Offline
                      miabaker
                      wrote on last edited by
                      #10

                      Hi, I think you need to use the QSqlQuery::bindValue() method instead of QSqlQuery::addBindValue(). The former binds a value to a named placeholder, while the latter binds a value to a positional placeholder. See the documentation for more details: https://doc.qt.io/qt-5/qsqlquery.html#bindValue

                      Regards, Mia from CodeIT

                      JonBJ 1 Reply Last reply
                      0
                      • M miabaker

                        Hi, I think you need to use the QSqlQuery::bindValue() method instead of QSqlQuery::addBindValue(). The former binds a value to a named placeholder, while the latter binds a value to a positional placeholder. See the documentation for more details: https://doc.qt.io/qt-5/qsqlquery.html#bindValue

                        Regards, Mia from CodeIT

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

                        @miabaker said in bindValue is not working:

                        Hi, I think you need to use the QSqlQuery::bindValue() method instead of QSqlQuery::addBindValue().

                        So far the OP has only used bindValue(), which is correct for named parameters, not addBindValue() at all which is only for positional parameters. So don't know what you are saying here.

                        1 Reply Last reply
                        1
                        • I icoicqico

                          @icoicqico said in bindValue is not working:

                          "INSERT INTO idcard_data (?) Values ('hello')"

                          This is the result of query.executedQuery(), or you want something else?

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

                          @icoicqico
                          If you want help, e.g. from @Christian-Ehrlicher or myself, let us know when you are prepared to make the changes we suggested, and not continue with something else which will never work. Up to you.

                          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