Convert QSqlQueryModel Data into QVector(s)



  • If we get data from an SQL database using the following QT functionality

    @ QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("SELECT name, salary FROM employee");@

    it states in the accompanying material we can access individual elements using either of the following commands

    @ int salary = model.record(4).value("salary").toInt();@

    or

    @ int salary = model.data(model.index(4, 2)).toInt();@

    and that we can insert this data into a QTableView using

    @ QTableView *view = new QTableView;
    view->setModel(model);@

    And that this data can also be However, I can see anywhere how to extract a whole column of data or range of columns / rows (or as in the terminology of QSqlQueryModel index & roles).

    Ultimately what I would like to do is efficiently extract parts of the data into either QVectors e.g column 3 of the data. or possibly into a matrix library such as armadillo or Eigen using some sort of getDataFunc(QVector rows, QVector columns).

    The data I am working is can be huge, so efficiently is important and thus using a naive set of for loops seems a very bad idea.

    Any help much appreciated.


  • Lifetime Qt Champion

    Hi,

    AFAIK there is no such concept (though the idea is interesting) out of the box from the QtSqlModule.

    Since this would involve a relatively low-level interaction, I would recommend that you ask this question on the interest mailing list, you'll find there Qt's developers/maintainers (this forum is more user oriented)



  • Thanks...

    The only other way I can think of doing it is using QSqlQuery functionality to get the data, store it in a certain form e.g. armadillo matrix, then use something like QAbstractTableModel to enable interaction of data with things like QTableView and maybe to QAbstractProxyModel to get various selections of the data.

    However, all of the above is very new to me, thus I was hoping that there was an easy way using the "models" provided for data such as sql query returns. Furthermore, I have no idea in terms of performance how such a solution would work.


  • Lifetime Qt Champion

    Then I might have misunderstood what you were asking, you can use QSqlQueryModel with Qt's view without any problem. QSqlTableModel is built on top of QSqlQueryModel



  • Yes I understand that...what I require is efficient access to the underlying data such that I can select columns from the QSqlQueryModel, convert them to say QVectors or a matrix, in order to run algorithms on them.

    It appears though that that is only possible via iterating through all the "records", rather than with most matrix data structures the ability to access columns directly.


  • Lifetime Qt Champion

    Then my suggestion about the mailing list is still valid.

    In the meantime, you could have look at how QSqlModel works and create your own model that would build a data structure that would follow your needs


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.