Search code examples
c++qt5qlist

Better way to store query results from db in Qt Lists


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?


Solution

  • On the Qt side, the only optimization that I can think of is

    querySelect.setForwardOnly(true)
    

    By the way, I think you can change approach and create a single class and a list for it, rather then three separated lists.

    struct Data
    {
        QString rfid;
        QDateTime dtIn;
        QDateTime dtOut;
    };
    

    Then store the results this way:

    QList<Data> list;
    while( querySelect.next() ) {    
        list.append( {querySelect.value(0).toString(),
                      querySelect.value(1).toDateTime(),
                      querySelect.value(2).toDateTime()} );
    }