QSqlQuery returns record not in right order.
-
wrote on 14 Jun 2013, 08:54 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 ? -
Just to amke sure: when you execute the same query in your sql browser the query/result is ok?
-
wrote on 14 Jun 2013, 09:03 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.
-
wrote on 14 Jun 2013, 09:19 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.
-
wrote on 14 Jun 2013, 09:39 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.
-
wrote on 14 Jun 2013, 12:18 last edited by
I remove QAplication::processEvents() from my loop and now it works as shoud.
-
wrote on 14 Jun 2013, 12:55 last edited by
As you are not showing any loop, that was kinda hard to spot for us...
-
wrote on 14 Jun 2013, 13:09 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/8