QSqlQuery returns record not in right order.

  • I execute some sql request using QSqlQuery. In text i put "ORDER BY SomeParam" in the end. Nex i make a loop:
    @while(query.next) {
    QSqlRecord record = query.record();
    // some code
    And my records comes not in that order as in sql table which i can see on my sql browser. What is the reason ?

  • Moderators

    Just to amke sure: when you execute the same query in your sql browser the query/result is ok?

  • bq. Just to amke sure: when you execute the same query in your sql browser the query/result is ok?bq.

    Thats right. Result in browser is in right order.

  • I'm with raven-worx here: please check the query you use very, very carefully. If you're building it up dynamically, check the end result SQL and re-run that same SQL in your database browser.

  • Query which i use i get from this:
    @ QSqlQuery query;
    bool bOk = AReportsModel::applyFilter(props, query);@

    In applyFilter method I do next :
    bool AReportsModel::applyFilter(const sxProperties & props, QSqlQuery & rQuery) {

    QSqlQuery query(GET_DATABASE()); 


    // post filter (applies only for new table)
    QString sPostFilter = getPostFilterString();
    bool bResult;
    if (sPostFilter.isEmpty())
        bResult = query.exec(QString("SELECT * FROM %1 ORDER BY %2").arg(tableName()).arg(sxDB::cmd_list::CMD_TIME)); 
        bResult = query.exec(QString("SELECT * FROM %1 WHERE %2 ORDER BY %3").arg(tableName()).arg(sPostFilter).arg(sxDB::cmd_list::CMD_TIME)); 
    rQuery = query;
    return bResult;

    Im using qt version 4.8.4 and PostgreSQL as database.

    I store QString value of query.lastQuery() before my while loop and execute it in SQL Browser and get right result.

  • I remove QAplication::processEvents() from my loop and now it works as shoud.

  • As you are not showing any loop, that was kinda hard to spot for us...

  • Here it is. I forgot to paste it in my first post
    @while(query.next) {

    QSqlRecord record = query.record();

    // some code


Log in to reply

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