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 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?

    Christian EhrlicherC 1 Reply Last reply
    0
    • D Donald9307

      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?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      0
      • Christian EhrlicherC Christian Ehrlicher

        @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 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.

        Christian EhrlicherC 1 Reply Last reply
        0
        • D Donald9307

          @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.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          1
          • Christian EhrlicherC Christian Ehrlicher

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

            D Offline
            D Offline
            Donald9307
            wrote on 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?

            SGaistS 1 Reply Last reply
            0
            • D Donald9307

              @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?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              1
              • SGaistS SGaist

                @Donald9307 hi,

                Did you call next once before getting the value ?

                D Offline
                D Offline
                Donald9307
                wrote on 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

                • Login

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