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. Better way to store query results from db in Qt Lists

Better way to store query results from db in Qt Lists

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 4.3k 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.
  • M mardzo

    I have a select query from which i want to store results in QT lists after query execution. Some of my code is:

    QSqlQuery querySelect;
    
    QStringList rfid;
    QList<QDateTime> datetimeIN;
    QList<QDateTime> datetimeOUT;
    
    ... Some other code + querySelect prepare.....
    
    if(querySelect.exec())  {
    
    
              while( querySelect.next() ) {
    
                    rfid.append(querySelect.value( 0 ).toString());
                    datetimeIN.append(querySelect.value( 1 ).toDateTime());
                    datetimeOUT.append(querySelect.value( 2 ).toDateTime());
    
              }
    
         }
         else  {
             
           qDebug()  << querySelect.lastError();
       
      }
    
    I want to know is there another, more optimized faster approach to store the results in the lists, other than with the query.next while cycle because i think this is rather slow?`
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @mardzo
    No, you do have to fetch each row out of the query result set one at a time and do whatever you want with the data. In terms of "slow", be aware that this is only doing the loop client-side, it isn't actually doing it one-at-a-time server-side (e.g. with a SQL CURSOR), so it's not that bad.

    As for your "style" of handling the rows returned by storing the column values into 3 separate list variables. This is a bit "messy", and relies on you keeping each list in sync with the others and using the same index for each one. Do you not think a "nicer" way to represent your rows fetched from the data source might be to create a class which has member variables for a single rfid, datatimeIN & datetimeOUT --- i.e. it represents a single row --- and then return a QList<RowClass>?

    1 Reply Last reply
    3
    • M Offline
      M Offline
      mardzo
      wrote on last edited by
      #3

      Yes for that you are right, but as this was only a test program for presentation, i will fix the above asap. Just wanted to tel that when I put querySelect.setForwardOnly(true); the data filling is much faster

      JonBJ 1 Reply Last reply
      0
      • M mardzo

        Yes for that you are right, but as this was only a test program for presentation, i will fix the above asap. Just wanted to tel that when I put querySelect.setForwardOnly(true); the data filling is much faster

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @mardzo said in Better way to store query results from db in Qt Lists:

        querySelect.setForwardOnly(true); the data filling is much faster

        http://doc.qt.io/qt-5/qsqlquery.html#setForwardOnly :

        Forward only mode can be (depending on the driver) more memory efficient since results do not need to be cached. It will also improve performance on some databases.

        I'm (really quite) surprised this makes much difference, and intrigued. Are you using MySQL, on what platform, or what? How big is your resultset from your query?

        Taz742T 1 Reply Last reply
        0
        • M Offline
          M Offline
          mardzo
          wrote on last edited by
          #5

          Im fetching results from a remote Microsoft SQL Server 2012 on a local network installed on a windows 10 machine. As this software is still in development i am currently fetching about 500 rows from 6 columns in a request

          JonBJ 1 Reply Last reply
          0
          • JonBJ JonB

            @mardzo said in Better way to store query results from db in Qt Lists:

            querySelect.setForwardOnly(true); the data filling is much faster

            http://doc.qt.io/qt-5/qsqlquery.html#setForwardOnly :

            Forward only mode can be (depending on the driver) more memory efficient since results do not need to be cached. It will also improve performance on some databases.

            I'm (really quite) surprised this makes much difference, and intrigued. Are you using MySQL, on what platform, or what? How big is your resultset from your query?

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by
            #6

            @mardzo
            We can not change the Qt base and its aspirations.
            The only thing we can do is keep this information on the Qt side so that further operations are quick.

            Which @JNBarchan said well:
            @JNBarchan said in Better way to store query results from db in Qt Lists:

            i.e. it represents a single row --- and then return a QList<RowClass>?

            QHash <QString, RowClass> will be faster then QList<RowClass> .

            For example:

            struct RowClass {
                QDateTime dtIN;
                QDateTime dtOUT;
                QString rfid;
            }
            
            QHash<QString, RowClass> hash;
            
            while( querySelect.next() ) {
                QString rfid = querySelect.value( 0 ).toString();
                QDateTime dtIN = querySelect.value( 1 ).toDateTime();
                QDateTime dtOUT = querySelect.value( 2 ).toDateTime();
                
                RowClass cls;
                cls.rfid = rfid;
                cls.dtIN = dtIN;
                cls.dtOUT = dtOUT;
            
                hash[rfid] = cls;
            }
            

            Do what you want.

            JonBJ 1 Reply Last reply
            0
            • Taz742T Taz742

              @mardzo
              We can not change the Qt base and its aspirations.
              The only thing we can do is keep this information on the Qt side so that further operations are quick.

              Which @JNBarchan said well:
              @JNBarchan said in Better way to store query results from db in Qt Lists:

              i.e. it represents a single row --- and then return a QList<RowClass>?

              QHash <QString, RowClass> will be faster then QList<RowClass> .

              For example:

              struct RowClass {
                  QDateTime dtIN;
                  QDateTime dtOUT;
                  QString rfid;
              }
              
              QHash<QString, RowClass> hash;
              
              while( querySelect.next() ) {
                  QString rfid = querySelect.value( 0 ).toString();
                  QDateTime dtIN = querySelect.value( 1 ).toDateTime();
                  QDateTime dtOUT = querySelect.value( 2 ).toDateTime();
                  
                  RowClass cls;
                  cls.rfid = rfid;
                  cls.dtIN = dtIN;
                  cls.dtOUT = dtOUT;
              
                  hash[rfid] = cls;
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Taz742

              QHash <QString, RowClass> will be faster then QList<RowClass> .
              hash[rfid] = cls;

              1. You are assuming that OP's rfid is a primary key (or at least unique). While that may be the case, the OP does not state that anywhere in his question.

              2. QHash will only be faster than QList if the OP does multiple lookups by key field in his results. If he does not --- e.g. he just uses the values once to populate the UI, say --- it will actually be (marginally) slower & more memory.

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mardzo
                wrote on last edited by mardzo
                #8

                Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                JonBJ Taz742T VRoninV 3 Replies Last reply
                0
                • M mardzo

                  Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @mardzo
                  Than as I said, stick with QList, not QHash, in your example.

                  You still have to fill the QStandardItemModel one row at a time, there is nothing more efficient than that offered from Qt. In this case, of course, there is no need to involve a QList, you do it straight from querySelect.next() into QStandardItemModel.setData().

                  1 Reply Last reply
                  0
                  • M mardzo

                    Im fetching results from a remote Microsoft SQL Server 2012 on a local network installed on a windows 10 machine. As this software is still in development i am currently fetching about 500 rows from 6 columns in a request

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

                    @mardzo said in Better way to store query results from db in Qt Lists:

                    Im fetching results from a remote Microsoft SQL Server 2012 on a local network installed on a windows 10 machine. As this software is still in development i am currently fetching about 500 rows from 6 columns in a request

                    I'm still surprised/intrigued that you say setForwardOnly() makes any (noticeable) difference to speed. Are you sure it does (i.e. you've run it a few times with and without)? (e..g It would be more likely that it's the "random" delay in response from remote SQL Server than the forward-only.) I'd be grateful if you confirmed this, as it might make a difference to me in code I have inherited --- thanks.

                    M 1 Reply Last reply
                    0
                    • M mardzo

                      Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #11

                      @mardzo said in Better way to store query results from db in Qt Lists:

                      Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                      When I use QTableWidget and fill it with my class. I use a 0-bit column for UID-like. When I need to edit or delete this record in the database, I have this record UID in the QTableWidget's last row and in the 0-van column and this class from QHash is quicker.
                      If you're the same, I recommend you use QHash.

                      @JNBarchan I wanted to say this from the beginning.

                      Do what you want.

                      JonBJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @mardzo said in Better way to store query results from db in Qt Lists:

                        Im fetching results from a remote Microsoft SQL Server 2012 on a local network installed on a windows 10 machine. As this software is still in development i am currently fetching about 500 rows from 6 columns in a request

                        I'm still surprised/intrigued that you say setForwardOnly() makes any (noticeable) difference to speed. Are you sure it does (i.e. you've run it a few times with and without)? (e..g It would be more likely that it's the "random" delay in response from remote SQL Server than the forward-only.) I'd be grateful if you confirmed this, as it might make a difference to me in code I have inherited --- thanks.

                        M Offline
                        M Offline
                        mardzo
                        wrote on last edited by
                        #12

                        @JNBarchan yes i have run it multiple times with and without and in I confirm in my case it makes a difference.

                        Cheers

                        1 Reply Last reply
                        0
                        • M mardzo

                          Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #13

                          Did you try running your program in Release mode? Debug mode for QtSql is quite heavy and might be the real bottleneck here (apart from network but that's not something you can control)

                          @mardzo said in Better way to store query results from db in Qt Lists:

                          Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel

                          Then why are you putting a further step inbetween? just fill the model in the query loop.

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          2
                          • Taz742T Taz742

                            @mardzo said in Better way to store query results from db in Qt Lists:

                            Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                            When I use QTableWidget and fill it with my class. I use a 0-bit column for UID-like. When I need to edit or delete this record in the database, I have this record UID in the QTableWidget's last row and in the 0-van column and this class from QHash is quicker.
                            If you're the same, I recommend you use QHash.

                            @JNBarchan I wanted to say this from the beginning.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #14

                            @Taz742 said in Better way to store query results from db in Qt Lists:

                            @mardzo said in Better way to store query results from db in Qt Lists:

                            Just to clarify, rfid is not a primary key, and once i fetch the data, i am using it to fill a QStandardItemmodel and display it in a QTableView in the ui

                            When I use QTableWidget and fill it with my class. I use a 0-bit column for UID-like. When I need to edit or delete this record in the database, I have this record UID in the QTableWidget's last row and in the 0-van column and this class from QHash is quicker.
                            If you're the same, I recommend you use QHash.

                            @JNBarchan I wanted to say this from the beginning.

                            Before we confuse the OP, he wants to use QStandardItemModel, and as @VRonin & I have said, he should not go via either QList or QHash, he should just fill the model in the query loop.

                            Next bit for @Taz742 only:

                            Assuming you only show, say, at most 100 rows to the user (you don't want to show thousands, do you), and do the occasional one-time lookup of a column I can't see it will be faster with a QHash than a QList (assuming you remember to count the time for populating the QHash as well as the single lookup). But whatever.

                            What I don't get, however, is why you're doing any lookup at all? From a QTableWidget/QTableView, isn't the whole point to use the QModelIndex functions to map directly from the view to the row in the model, and use that, without any searching?

                            1 Reply Last reply
                            1

                            • Login

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