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. QSqlQuery returns record not in right order.

QSqlQuery returns record not in right order.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.0k 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    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
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        Anticross
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            Anticross
            wrote on last edited by
            #5

            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
            0
            • A Offline
              A Offline
              Anticross
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Anticross
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

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