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 numRowsAffected() vs. size()
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery numRowsAffected() vs. size()

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 487 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.
  • Y Offline
    Y Offline
    ymhsieh
    wrote on last edited by
    #1

    Dear All,
    I have been playing with SQL capabilities of Qt. According to the documentation (https://doc.qt.io/qt-6/qsqlquery.html), for select statements, size() returns the number of rows returned, and numRowsAffected() is not defined. But this seems incorrect from my experiments on a simple select statement (using QODBC connecting to MS-SQL 2022), the numRowsAffected() returns the number of rows returned from the select statement, and size() returns -1. Is it possible there is a bug in Qt or in the documentation?

    Sincerely,.
    Yo-Ming Hsieh

    Christian EhrlicherC 1 Reply Last reply
    0
    • Y ymhsieh

      Dear All,
      I have been playing with SQL capabilities of Qt. According to the documentation (https://doc.qt.io/qt-6/qsqlquery.html), for select statements, size() returns the number of rows returned, and numRowsAffected() is not defined. But this seems incorrect from my experiments on a simple select statement (using QODBC connecting to MS-SQL 2022), the numRowsAffected() returns the number of rows returned from the select statement, and size() returns -1. Is it possible there is a bug in Qt or in the documentation?

      Sincerely,.
      Yo-Ming Hsieh

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ymhsieh said in QSqlQuery numRowsAffected() vs. size():

      nd size() returns -1. Is it possible there is a bug in Qt or in the documentation?

      No, but you did not read the doc completely: "Note that for SELECT statements, the value is undefined; use size() instead. If the query is not active, -1 is returned."

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

      Y 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @ymhsieh said in QSqlQuery numRowsAffected() vs. size():

        nd size() returns -1. Is it possible there is a bug in Qt or in the documentation?

        No, but you did not read the doc completely: "Note that for SELECT statements, the value is undefined; use size() instead. If the query is not active, -1 is returned."

        Y Offline
        Y Offline
        ymhsieh
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        "No, but you did not read the doc completely: "Note that for SELECT statements, the value is undefined; use size() instead. If the query is not active, -1 is returned."
        This is exactly the statement that confuses me. I am pretty certain that I have an active select statement (because I can get my rows), so size() should give me the number of rows, but it returns -1. While numRowsAffected() should be undefined, but it gives me exactly the number of rows that I can retrieve... :-(

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          Well, in C++ there is no such value as undefined. "Undefined" just means that what value should be returned is not defined in design.
          So it may return the value you want, or it may return invalid value, or any value, usually we should not depend on that.

          As for the size() function, the doc also write that it returns

          -1 if the size cannot be determined or if the database does not support reporting information about query sizes

          In your case, ODBC databases do not support reporting the size.
          You can confirm that by calling driver()->hasFeature(QSqlDriver::QuerySize).

          Y 1 Reply Last reply
          1
          • B Bonnie

            Well, in C++ there is no such value as undefined. "Undefined" just means that what value should be returned is not defined in design.
            So it may return the value you want, or it may return invalid value, or any value, usually we should not depend on that.

            As for the size() function, the doc also write that it returns

            -1 if the size cannot be determined or if the database does not support reporting information about query sizes

            In your case, ODBC databases do not support reporting the size.
            You can confirm that by calling driver()->hasFeature(QSqlDriver::QuerySize).

            Y Offline
            Y Offline
            ymhsieh
            wrote on last edited by
            #5

            @Bonnie Great, thank you for your explanation, although I still find it weird that numRowsAffected() gives me the number of rows ...XD

            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