QSqlQuery for SQLite在向前迭代中使用next()只会找到一行 [英] QSqlQuery for SQLite in forward iteration with next() will only find one row

查看:1900
本文介绍了QSqlQuery for SQLite在向前迭代中使用next()只会找到一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题已经不时讨论。然而,从来没有解决这个问题,它自己。正如我发现,向前和向后迭代行有一个区别。使用向前迭代 c> 可以仅产生一行,而使用 QSqlQuery :: previous ()始终找到所有行。



编辑: 已删除的参考



关于Qt文档,正确的方法如下:

  QSqlQuery q = db。 exec(SELECT * FROM Table); 
while(q.next()){
//使用行执行操作...
}

但是,这将导致只有一行。向后迭代将找到所有行。

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

向后迭代可能非常缓慢,应该避免。有人知道为什么这在前向迭代中不起作用吗?



编辑:这种行为不总是可重现的,有时不是。在这种情况下,数据库包含更多行。问题恰好出现在此代码段中。是否有任何一个具有相同的问题?



编辑2:似乎该bug已在Qt 4.8.5修复。 p>

我当前的环境:Windows 7(SP1),Qt 4.8.5。

解决方案

这是错误的,下面的问题已经在这里讨论了几次。您提供的参考资料与您所看到的行为有无关



您可能在替换 //代码中使用row ... 的代码中有错误。您需要显示该代码的完整。您也可以简单地看到,在问题发生时,您的表格中只有一行表示 。这将是由于与修改数据库的其他代码的交互,但是你永远不会共享该代码,所以我们怎么知道?



如果你只是向前迭代,你应该调用 q.setForwardOnly(true),因为这会使事情在一些数据库后端更快。


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.

解决方案

It is simply false that the following problem has been discussed here several times. The references you provide have nothing to do with the behavior that you're seeing.

You likely have bugs in the code that replaces // Do something with row.... You need to show that code in its entirety. You may also simply see the fact that there is in fact just a single row in your table at the time the problem occurs. This would be due to interaction with other code that modifies the database, but you never share that code, so how can we know?

If you're only iterating forward, you should call q.setForwardOnly(true) as this will make things faster on some database backends.

这篇关于QSqlQuery for SQLite在向前迭代中使用next()只会找到一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆