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 bindValue is not working properly
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery bindValue is not working properly

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 12.7k Views 2 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Which version of Qt are you using ?

    IIRC, named placeholder with SQLite will work properly with Qt 5.10.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    S 1 Reply Last reply
    2
    • C Offline
      C Offline
      Chrisw01
      wrote on last edited by
      #3

      Hi,

      There doesn't appear to be anything wrong with your SQL code. I wrote a little test program with 5.7.0 using MinGW and it works fine.

      Here is the test code.

      QSqlDatabase db;
          db=QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName("TEST.DB");
          db.open();
          QSqlQuery sql;
          if(db.isValid()) {
              if(db.tables().isEmpty()) {
                  sql.exec("CREATE TABLE \"users\" ( `ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `username` TEXT NOT NULL, `pass` TEXT NOT NULL, `userGroup` INTEGER NOT NULL)");
                  if(sql.numRowsAffected() == 1) {
                      qDebug() << "Database Created!";
                  }
              }
              sql.prepare("INSERT INTO users (username, pass, userGroup) VALUES (:name, :pass, :group)");
              sql.bindValue(":name", "Test Name");
              sql.bindValue(":pass", "Test Pass");
              sql.bindValue(":group", "0");
              sql.exec();
              if(sql.numRowsAffected() != 1) {
                  qDebug() << sql.lastError();
                  qDebug() << sql.lastQuery();
              }
              else qDebug() << "Num Rows Affected : " << sql.numRowsAffected();
          }
          else qDebug() << db.lastError();
      

      Ran it over and over again always successful. I've been using QtSql since the 4.8 days always seems to work fine.. IIRC I did have database issues way back when with the Microsoft Compilers...

      hope it helps.

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #4

        It might be the specific driver you are using. can you replace named binding with positional binding? see http://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values

        A quick way to check why I suspect this is a problem is qDebug() << db.driver()->hasFeature(QSqlDriver::NamedPlaceholders);. if it returns false, Qt will try to do some magic to make it work anyway and sometimes it fails like in your case

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        S 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Which version of Qt are you using ?

          IIRC, named placeholder with SQLite will work properly with Qt 5.10.

          S Offline
          S Offline
          Script22
          wrote on last edited by
          #5

          @SGaist I'm using Qt 5.9 on manjaro Linux

          1 Reply Last reply
          0
          • VRoninV VRonin

            It might be the specific driver you are using. can you replace named binding with positional binding? see http://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values

            A quick way to check why I suspect this is a problem is qDebug() << db.driver()->hasFeature(QSqlDriver::NamedPlaceholders);. if it returns false, Qt will try to do some magic to make it work anyway and sometimes it fails like in your case

            S Offline
            S Offline
            Script22
            wrote on last edited by
            #6

            @VRonin said in QSqlQuery bindValue is not working properly:

            qDebug() << db.driver()->hasFeature(QSqlDriver::NamedPlaceholders);

            It did return false, but no magic happened :)
            Thanks ! I will try to figure out a work around it!

            VRoninV 1 Reply Last reply
            0
            • S Script22

              @VRonin said in QSqlQuery bindValue is not working properly:

              qDebug() << db.driver()->hasFeature(QSqlDriver::NamedPlaceholders);

              It did return false, but no magic happened :)
              Thanks ! I will try to figure out a work around it!

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #7

              @Script22 said in QSqlQuery bindValue is not working properly:

              I will try to figure out a work around it!

              The workaround is positional binding: useing (?,?,?) instead of (:name, :pass, :group)

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              S 1 Reply Last reply
              1
              • VRoninV VRonin

                @Script22 said in QSqlQuery bindValue is not working properly:

                I will try to figure out a work around it!

                The workaround is positional binding: useing (?,?,?) instead of (:name, :pass, :group)

                S Offline
                S Offline
                Script22
                wrote on last edited by
                #8

                @VRonin Tried that but it did not work either, I'm currently using an naive way via QString("query....").args()

                VRoninV 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Did you got the same error ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Script22

                    @VRonin Tried that but it did not work either, I'm currently using an naive way via QString("query....").args()

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #10

                    @Script22 said in QSqlQuery bindValue is not working properly:

                    I'm currently using an naive way via QString("query....").args()

                    That is crazy dangerous for sql injection. make sure you use db.driver()->escapeIdentifier() to prevent it

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    2
                    • S Offline
                      S Offline
                      Script22
                      wrote on last edited by
                      #11

                      @VRonin said in QSqlQuery bindValue is not working properly:

                      @Script22 said in QSqlQuery bindValue is not working properly:

                      I will try to figure out a work around it!

                      The workaround is positional binding: useing (?,?,?) instead of (:name, :pass, :group)

                      My Bad, the question mark method is working
                      I knew the danger, I'm just working on a demo for now
                      In the future I will be using MySql
                      Thanks everyone @VRonin @SGaist

                      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