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

From time to time, the following problem was discussed. However, there was never a solution to the problem. As I found out, there is a difference in repeating lines back and forth. Iterating forward with QSqlQuery::next() can lead to only one row, however, reverse iteration with QSqlQuery::previous() will always find all rows. Whether the forward iteration is explicitly set or not has no effect.

Edit: Deleted Links

Regarding the Qt documentation, the correct approach would be:

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

However, this will result in only one line. The reverse iteration will contain all the lines.

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

Reverse iteration can be very slow and should be avoided. Does anyone know why this is not working in advanced iteration?

Edit: This behavior is not always reproducible for me, and sometimes it happens, sometimes not. The database contains more than one row in this case. The problem arises precisely in this piece of code. Is there anyone with the same problem?

Edit 2: It looks like the bug was fixed in Qt 4.8.5.

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

+4
source share
1 answer

It is simply not true that the following problem has been discussed here several times. The links you provided have nothing to do with the behavior you see.

You probably have errors in the code that replaces // Do something with row... You need to show this code as a whole. You can also just see the fact that at the time the problem occurs, there is only one row in your table. This is due to interacting with other code that modifies the database, but you never share this code, so how can we know?

If you are only iterating forward, you should call q.setForwardOnly(true) , as this will speed up work on some database backends.

-1
source

Source: https://habr.com/ru/post/1485810/


All Articles