cursor.rowcount总是-1在python3k中的sqlite3 [英] cursor.rowcount always -1 in sqlite3 in python3k
问题描述
我试图获得 sqlite3
游标$ c>的
rowcount
$ c>在我的Python3k程序,但我很困惑,因为 rowcount
总是 -1
docs说(实际上是矛盾的,应该是 None
)。即使在获取所有行之后, rowcount
仍保留在 -1
。是 sqlite3
错误吗?我已经检查表中是否有行。
I am trying to get the rowcount
of a sqlite3
cursor
in my Python3k program, but I am puzzled, as the rowcount
is always -1
, despite what Python3 docs say (actually it is contradictory, it should be None
). Even after fetching all the rows, rowcount
stays at -1
. Is it a sqlite3
bug? I have already checked if there are rows in the table.
我可以通过检查 fetchone()
返回不同于 None
的东西,但我认为这个问题很好讨论。
I can get around this checking if a fetchone()
returns something different than None
, but I thought this issue would be nice to discuss.
p>
推荐答案
从文档:
根据Python DB API Spec,
rowcount属性是-1,如果
没有对
执行executeXX()游标或最后
操作的rowcount不能由
接口确定
As required by the Python DB API Spec, the rowcount attribute "is -1 in case no executeXX() has been performed on the cursor or the rowcount of the last operation is not determinable by the interface".
这包括 SELECT
语句
,因为我们无法确定数字
这意味着 all SELECT
语句不会有 rowcount
。你观察的行为是记录在案的。
That means all SELECT
statements won't have a rowcount
. The behaviour you're observing is documented.
编辑: rowcount
/ strong>在你做一个 fetchall()
后更新,所以这是错误的假设。
Documentation doesn't say anywhere that rowcount
will be updated after you do a fetchall()
so it is just wrong to assume that.
这篇关于cursor.rowcount总是-1在python3k中的sqlite3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!