Home:ALL Converter>QSqlQuery for SQLite in forward iteration with next() will only find one row

QSqlQuery for SQLite in forward iteration with next() will only find one row

Ask Time:2013-06-12T21:05:58         Author:bkausbk

Json Formatter

The following problem has been discussed from time to time. However there was never a solution for the problem it self. As I found out there is a difference in iterating rows forward and backward. Forward iteration with QSqlQuery::next() may result in only one row, however backward iteration with QSqlQuery::previous() will always find all rows. Whether forward iteration is set explicitely or not, has no effect.

Edit: References removed

Concerning Qt documentation the right approach would be following:

QSqlQuery q = db.exec("SELECT * FROM Table");
while (q.next()) {
    // Do something with row...
}

However this will result in only one row. Backward iteration will find all rows.

QSqlQuery q = db.exec("SELECT * FROM Table");
if (q.last()) {
    do {
        // Do something with row...
    } while (q.previous());
}

Backward iteration could be very slow and should be avoided. Has anyone an idea why this does not work in forward iteration?

Edit: This behavior is not always reproduceable to me and sometimes it happens, sometimes not. The database contains more than one row in this case. The problem occurs exactly in this code snippets. Is there any one with the same problem?

Edit 2: It seems that the bug has been fixed in Qt 4.8.5.

My current environment: Windows 7 (SP1), Qt 4.8.5.

Author:bkausbk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17066315/qsqlquery-for-sqlite-in-forward-iteration-with-next-will-only-find-one-row
yy