Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    QSqlQuery returns record not in right order.

    General and Desktop
    3
    8
    1825
    Loading More Posts
    • 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.
    • A
      Anticross last edited by

      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 ?

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • A
          Anticross last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • A
              Anticross last edited by

              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()); 
              

              query.clear();

              // 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)); 
              else
                  bResult = query.exec(QString("SELECT * FROM %1 WHERE %2 ORDER BY %3").arg(tableName()).arg(sPostFilter).arg(sxDB::cmd_list::CMD_TIME)); 
              
              setQuery(query);
              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.

              1 Reply Last reply Reply Quote 0
              • A
                Anticross last edited by

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

                1 Reply Last reply Reply Quote 0
                • A
                  andre last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • A
                    Anticross last edited by

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

                    QAplication::processEvents();
                    QSqlRecord record = query.record();

                    // some code

                    }@

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post