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

Detect if SQLite UPDATE statement does not actually update anything

Scheduled Pinned Locked Moved General and Desktop
sqlsqliteqsqlquery
8 Posts 3 Posters 3.9k 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 3 Dec 2016, 21:20 last edited by Kofr 12 Mar 2016, 21:21
    #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.

    P 1 Reply Last reply 3 Dec 2016, 21:27
    0
    • K Kofr
      3 Dec 2016, 21:20

      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.

      P Offline
      P Offline
      Paul Colby
      wrote on 3 Dec 2016, 21:27 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 3 Dec 2016, 21:40
      1
      • P Paul Colby
        3 Dec 2016, 21:27

        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 3 Dec 2016, 21:40 last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Dec 2016, 23:22 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 4 Dec 2016, 22:43
          1
          • S SGaist
            3 Dec 2016, 23:22

            Hi,

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

            K Offline
            K Offline
            Kofr
            wrote on 4 Dec 2016, 22:43 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 4 Dec 2016, 22:46 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 6 Dec 2016, 14:42
              0
              • S SGaist
                4 Dec 2016, 22:46

                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 6 Dec 2016, 14:42 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
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Dec 2016, 22:55 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

                  1/8

                  3 Dec 2016, 21:20

                  • Login

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