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. Detect if SQLite UPDATE statement does not actually update anything
Qt 6.11 is out! See what's new in the release blog

Detect if SQLite UPDATE statement does not actually update anything

Scheduled Pinned Locked Moved General and Desktop
sqlsqliteqsqlquery
8 Posts 3 Posters 5.3k Views 4 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1

    I use statement like this

    QString queryContent = "UPDATE \'" + parentName + "\' SET " + column + " = :value WHERE row = " + QString::number(t_row);
    
    

    Row is the Primary Index.
    When I execute query with for example t_row = 60 and such table contains 55 row, the UPDATE statement is executed successfully with no errors, but does not updait anything.
    How to make SQL query fail in such case, so I can detect the incorrect data?
    ps. UPDATE OR FAIL does not help in case with WHERE filtering.

    Paul ColbyP 1 Reply Last reply
    0
    • K Kofr

      I use statement like this

      QString queryContent = "UPDATE \'" + parentName + "\' SET " + column + " = :value WHERE row = " + QString::number(t_row);
      
      

      Row is the Primary Index.
      When I execute query with for example t_row = 60 and such table contains 55 row, the UPDATE statement is executed successfully with no errors, but does not updait anything.
      How to make SQL query fail in such case, so I can detect the incorrect data?
      ps. UPDATE OR FAIL does not help in case with WHERE filtering.

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @Kofr,

      ... the UPDATE statement is executed successfully with no errors, but does not updait anything.
      How to make SQL query fail in such case, so I can detect the incorrect data?

      You can use QSqlQuery::numRowsAffected(), eg:

      if (query.numRowsAffected() < 1) {
          // no rows were updated - handle this however you like.
      }
      

      Cheers.

      K 1 Reply Last reply
      1
      • Paul ColbyP Paul Colby

        Hi @Kofr,

        ... the UPDATE statement is executed successfully with no errors, but does not updait anything.
        How to make SQL query fail in such case, so I can detect the incorrect data?

        You can use QSqlQuery::numRowsAffected(), eg:

        if (query.numRowsAffected() < 1) {
            // no rows were updated - handle this however you like.
        }
        

        Cheers.

        K Offline
        K Offline
        Kofr
        wrote on last edited by
        #3

        @Paul-Colby thx for the answer. QSqlQuery::numRowsAffected() does not work with SQLite

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

          Hi,

          Strange, this is implemented in the plugin. Which version of Qt are you using ?

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

          K 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Strange, this is implemented in the plugin. Which version of Qt are you using ?

            K Offline
            K Offline
            Kofr
            wrote on last edited by
            #5

            @SGaist I use Qt 5.7.0
            query.numRowsAffected() returns -1
            and driver.hasFeature(QSqlDriver::QuerySize) returns false

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

              QuerySize is not the same thing as numRowsAffected.

              QuerySize is for whether the driver is able to give the number of rows returned by a query.

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

              K 1 Reply Last reply
              0
              • SGaistS SGaist

                QuerySize is not the same thing as numRowsAffected.

                QuerySize is for whether the driver is able to give the number of rows returned by a query.

                K Offline
                K Offline
                Kofr
                wrote on last edited by
                #7

                @SGaist there is no other argument in hasFeature(QSqlDriver::???) for checking for numRowsAffected()
                and query.numRowsAffected() returns -1 all the time even if UPDATE's succesful. So far in my case.

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

                  Can you show how you setup and run your query ?

                  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
                  1

                  • Login

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