Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to access Sqlite3 aggregate recordset
QtWS25 Last Chance

How to access Sqlite3 aggregate recordset

Scheduled Pinned Locked Moved Solved Qt 6
qt6select sumsqlite3
7 Posts 3 Posters 681 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.
  • D Offline
    D Offline
    Donald9307
    wrote on 13 May 2023, 01:26 last edited by
    #1

    I have a sqlite3 database with a column that holds QString data representing numbers. I’m able to execute the following aggregate SQL Select statement and obtain a recordset (single value).

    SELECT SUM(amount)
    FROM transactions
    WHERE bkRef = 1;

    bkRef is an Int datatype, and amount is a QString datatype.

    I can successfully execute the above query in Qt6 but cannot figure out how to access the result of the query so I can pass the value to a lineEdit. Can anyone help me with this?

    C 1 Reply Last reply 13 May 2023, 04:44
    0
    • D Donald9307
      13 May 2023, 01:26

      I have a sqlite3 database with a column that holds QString data representing numbers. I’m able to execute the following aggregate SQL Select statement and obtain a recordset (single value).

      SELECT SUM(amount)
      FROM transactions
      WHERE bkRef = 1;

      bkRef is an Int datatype, and amount is a QString datatype.

      I can successfully execute the above query in Qt6 but cannot figure out how to access the result of the query so I can pass the value to a lineEdit. Can anyone help me with this?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 May 2023, 04:44 last edited by
      #2

      @Donald9307 said in How to access Sqlite3 aggregate recordset:

      I can successfully execute the above query in Qt6 but cannot figure out how to access the result of the query

      See https://doc.qt.io/qt-6/qsqlquery.html#details

      QSqlQuery query("SELECT country FROM artist");
      while (query.next()) {
          QString country = query.value(0).toString();
          doSomething(country);
      }
      

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

      D 1 Reply Last reply 13 May 2023, 15:21
      0
      • C Christian Ehrlicher
        13 May 2023, 04:44

        @Donald9307 said in How to access Sqlite3 aggregate recordset:

        I can successfully execute the above query in Qt6 but cannot figure out how to access the result of the query

        See https://doc.qt.io/qt-6/qsqlquery.html#details

        QSqlQuery query("SELECT country FROM artist");
        while (query.next()) {
            QString country = query.value(0).toString();
            doSomething(country);
        }
        
        D Offline
        D Offline
        Donald9307
        wrote on 13 May 2023, 15:21 last edited by Donald9307
        #3

        @Christian-Ehrlicher Thank you for your reply. What you provided isn't what I need. I know how to access a particular column in my database from the query example you gave. Rather, I need to access the aggregate function (SUM) results from my SQL query. My query creates a virtual recordset "sum(amount)," containing one data element, namely, the sum of the amount column where the bkRef = 1. I would be most grateful if you could help me to figure that out.

        C 1 Reply Last reply 13 May 2023, 15:35
        0
        • D Donald9307
          13 May 2023, 15:21

          @Christian-Ehrlicher Thank you for your reply. What you provided isn't what I need. I know how to access a particular column in my database from the query example you gave. Rather, I need to access the aggregate function (SUM) results from my SQL query. My query creates a virtual recordset "sum(amount)," containing one data element, namely, the sum of the amount column where the bkRef = 1. I would be most grateful if you could help me to figure that out.

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 13 May 2023, 15:35 last edited by
          #4

          It's query.value(0) - the first item in the first record set.

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

          D 1 Reply Last reply 13 May 2023, 17:23
          1
          • C Christian Ehrlicher
            13 May 2023, 15:35

            It's query.value(0) - the first item in the first record set.

            D Offline
            D Offline
            Donald9307
            wrote on 13 May 2023, 17:23 last edited by
            #5

            @Christian-Ehrlicher when my query executes and I invoke query.value(0) I get the following error message:

            QSqlQuery::value: not positioned on a valid record

            Apparently the recordset returned from the SUM aggregate function is not associated with query.value(int). Do you have any other suggestions to try?

            S 1 Reply Last reply 13 May 2023, 18:21
            0
            • D Donald9307
              13 May 2023, 17:23

              @Christian-Ehrlicher when my query executes and I invoke query.value(0) I get the following error message:

              QSqlQuery::value: not positioned on a valid record

              Apparently the recordset returned from the SUM aggregate function is not associated with query.value(int). Do you have any other suggestions to try?

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 13 May 2023, 18:21 last edited by
              #6

              @Donald9307 hi,

              Did you call next once before getting the value ?

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

              D 1 Reply Last reply 13 May 2023, 19:41
              1
              • S SGaist
                13 May 2023, 18:21

                @Donald9307 hi,

                Did you call next once before getting the value ?

                D Offline
                D Offline
                Donald9307
                wrote on 13 May 2023, 19:41 last edited by
                #7

                @SGaist Hi. No I didn't. That fixed the problem I was having. Thanks so much for your help.

                1 Reply Last reply
                0
                • D Donald9307 has marked this topic as solved on 13 May 2023, 19:42

                6/7

                13 May 2023, 18:21

                • Login

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