Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Detect if SQLite UPDATE statement does not actually update anything

    General and Desktop
    sql sqlite qsqlquery
    3
    8
    3293
    Loading More Posts
    • 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
      Kofr last edited by 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 Colby 1 Reply Last reply Reply Quote 0
      • Paul Colby
        Paul Colby @Kofr last edited by

        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 Reply Quote 1
        • K
          Kofr @Paul Colby last edited by

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

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • K
              Kofr @SGaist last edited by

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

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • K
                  Kofr @SGaist last edited by

                  @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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 1
                    • First post
                      Last post