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 - clear bound values
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery - clear bound values

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 596 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.
  • AlveinA Offline
    AlveinA Offline
    Alvein
    wrote on last edited by Alvein
    #1

    Hello,

    Let's say we have something like this

    QSqlQuery qrySample;
    

    And prepare the query this way

    qrySample.prepare(sSomeParameterizedSQL);
    

    And use it this way

    qrySample.addBindValue(iSomeIntValue);
    qrySample.exec();
    

    What's the correct way of reusing the already prepared query?

    Like this??

    qrySample.boundValues().clear();
    qrySample.addBindValue(iSomeOTHERIntValue);
    qrySample.exec();
    

    Thanks for your comments. =)

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

      After exec() you can simply bind your new values.

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

      AlveinA 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        After exec() you can simply bind your new values.

        AlveinA Offline
        AlveinA Offline
        Alvein
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in QSqlQuery - clear bound values:

        After exec() you can simply bind your new values.

        Just checked and exec() does not clear the bindings, which is useful to repeat queries .

        But also, like you say, it's possible to just bind again.

        I find this behavior a little annoying.

        Thanks for your comment.

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

          @Alvein said in QSqlQuery - clear bound values:

          I find this behavior a little annoying.

          In which way?

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

          AlveinA 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @Alvein said in QSqlQuery - clear bound values:

            I find this behavior a little annoying.

            In which way?

            AlveinA Offline
            AlveinA Offline
            Alvein
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in QSqlQuery - clear bound values:

            @Alvein said in QSqlQuery - clear bound values:

            I find this behavior a little annoying.

            In which way?

            The way that bindings are not reset but you can bind again as if they were.

            If you bind two parameters and after the first exec() you bind only one...

            qrySample.addBindValue(iSomeIntValue);
            qrySample.addBindValue(iOtherIntValue);
            qrySample.exec();
            

            ...the next exec() will succeed because a previous parameter (iOtherIntValue in this sample) was already in the second position.

            qrySample.addBindValue(iYetAnotherIntValue);
            qrySample.exec();
            

            That's not intuitive. Binding again should require some sort of resetting. But I'm not blaming this class because I don't know if every supported database type works the same way, and I don't have the time to check that myself.

            BTW, the above sample is definitely a poor programming practice. It's just there to show what I'm talking about.

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

              For example the windows odbc api behaves exactly the same. You have to take care what you're doing.

              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

              • Login

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