Python MysqlDB 使用cursor.rowcount 和 SSDictCursor 返回错误的计数
我有以下代码
cur = db.cursor(cursors.SSDictCursor)
cur.execute("SELECT * FROM large_table")
result_count = cur.rowcount
print result_count
这会打印数字18446744073709551615
,这显然是错误的。如果我删除cursors.SSDictCursor,则会显示正确的数字。谁能告诉我如何在保留 SSDictCursor 的同时获取返回的记录数?
I have the following code
cur = db.cursor(cursors.SSDictCursor)
cur.execute("SELECT * FROM large_table")
result_count = cur.rowcount
print result_count
This prints the number 18446744073709551615
which is obviously wrong. If I remove the cursors.SSDictCursor
the correct number is shown. Can anyone tell me how I can get the number of records returned while keeping the SSDictCursor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取
SSDictCursor
或SSCursor
返回的记录数,您唯一的选择是:获取整个结果并使用
len() 进行计数,这违背了使用
SSDictCursor
或SSCursor
的初衷;在迭代时自己计算行数,这意味着直到结束时您才会知道计数(不太实用);或者,
运行一个额外的单独的
COUNT(*)
查询。我强烈推荐第三种选择。如果您所做的只是
SELECT COUNT(*) FROM table;
,那么速度会非常快。对于一些更复杂的查询来说,它会更慢,但是通过适当的索引,它对于大多数用途来说仍然足够快。顺便说一句,您看到的返回值在某种程度上是正确的;至少,就 MySQL C API 而言。
根据 PEP 249 中定义的 Python DB API,如果接口无法确定最后一次操作的rowcount。 @glglgl 解释了为什么在他们的答案中无法确定行数:
换句话说,服务器不知道它最终要获取多少行。当您执行查询时,MySQLdb 存储
mysql_affected_rows()
在游标的rowcount
属性中。由于计数不确定,因此此函数返回-1
作为无符号 long long 整数 (my_ulonglong
),标准库的ctypes
模块中提供的数字类型:A快速而肮脏的替代方案对于
ctypes
,当您知道您将始终处理 64 位无符号整数时,是:如果
MySQLdb
检查此返回值并给您,那就太好了您期望看到的有符号整数,但不幸的是它没有。To get the number of records returned by
SSDictCursor
orSSCursor
, your only options are:Fetch the entire result and count it using
len()
, which defeats the purpose of usingSSDictCursor
orSSCursor
in the first place;Count the rows yourself as you iterate through them, which means you won't know the count until hit the end (not likely to be practical); or,
Run an additional, separate
COUNT(*)
query.I highly recommend the third option. It's extremely fast if all you're doing is
SELECT COUNT(*) FROM table;
. It would be slower for some more complex query, but with proper indexing it should still be quick enough for most purposes.As an aside, the return value you're seeing is sort of correct; at least, as far as the MySQL C API is concerned.
Per the Python DB API defined in PEP 249, the rowcount attribute is -1 if the rowcount of the last operation cannot be determined by the interface. @glglgl explained why the rowcount can't be determined in their answer:
In other words, the server doesn't know how many rows it's ultimately going to fetch. When you execute a query,
MySQLdb
stores the return value ofmysql_affected_rows()
in the cursor'srowcount
attribute. Because the count is indeterminate, this function returns-1
as an unsigned long long integer (my_ulonglong
), a numeric type that's available in thectypes
module of the standard library:A quick-and-dirty alternative to
ctypes
, when you know you'll always be dealing with a 64-bit unsigned integer, is:It would be great if
MySQLdb
checked for this return value and gave you the signed integer you expect to see, but unfortunately it doesn't.对于
SSDictCursor
,该值只能读取。确定光标何时用完。在内部,
SSDictCursor
使用mysql_use_result()
允许服务器在获取完成之前开始传输数据。With a
SSDictCursor
, this value can only be read resp. determined when the cursor is used up.Internally,
SSDictCursor
usesmysql_use_result()
which allows the server to start transferring the data before the acquiring is complete.