Python MysqlDB 使用cursor.rowcount 和 SSDictCursor 返回错误的计数

发布于 2025-01-01 01:07:44 字数 294 浏览 0 评论 0原文

我有以下代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2025-01-08 01:07:45

要获取 SSDictCursorSSCursor 返回的记录数,您唯一的选择是:

  1. 获取整个结果并使用 len() 进行计数,这违背了使用 SSDictCursorSSCursor 的初衷;

  2. 在迭代时自己计算行数,这意味着直到结束时您才会知道计数(不太实用);或者,

  3. 运行一个额外的单独的 COUNT(*) 查询。

我强烈推荐第三种选择。如果您所做的只是SELECT COUNT(*) FROM table;,那么速度会非常快。对于一些更复杂的查询来说,它会更慢,但是通过适当的索引,它对于大多数用途来说仍然足够快。


顺便说一句,您看到的返回值在某种程度上是正确的;至少,就 MySQL C API 而言。

根据 PEP 249 中定义的 Python DB API,如果接口无法确定最后一次操作的rowcount。 @glglgl 解释了为什么在他们的答案中无法确定行数:

在内部,SSDictCursor使用mysql_use_result(),它允许服务器在获取完成之前开始传输数据。

换句话说,服务器不知道它最终要获取多少行。当您执行查询时,MySQLdb 存储 mysql_affected_rows() 在游标的 rowcount 属性中。由于计数不确定,因此此函数返回 -1 作为无符号 long long 整数 (my_ulonglong),标准库的 ctypes 模块中提供的数字类型:

>>> from ctypes import c_ulonglong
>>> n = c_ulonglong(-1)
>>> n.value
18446744073709551615L

A快速而肮脏的替代方案对于ctypes,当您知道您将始终处理 64 位无符号整数时,是:

>>> -1 & 0xFFFFFFFFFFFFFFFF
18446744073709551615L

如果 MySQLdb 检查此返回值并给您,那就太好了您期望看到的有符号整数,但不幸的是它没有。

To get the number of records returned by SSDictCursor or SSCursor, your only options are:

  1. Fetch the entire result and count it using len(), which defeats the purpose of using SSDictCursor or SSCursor in the first place;

  2. 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,

  3. 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:

Internally, SSDictCursor uses mysql_use_result() which allows the server to start transferring the data before the acquiring is complete.

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 of mysql_affected_rows() in the cursor's rowcount 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 the ctypes module of the standard library:

>>> from ctypes import c_ulonglong
>>> n = c_ulonglong(-1)
>>> n.value
18446744073709551615L

A quick-and-dirty alternative to ctypes, when you know you'll always be dealing with a 64-bit unsigned integer, is:

>>> -1 & 0xFFFFFFFFFFFFFFFF
18446744073709551615L

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.

倾城泪 2025-01-08 01:07:45

对于SSDictCursor,该值只能读取。确定光标何时用完。

在内部,SSDictCursor 使用 mysql_use_result() 允许服务器在获取完成之前开始传输数据。

With a SSDictCursor, this value can only be read resp. determined when the cursor is used up.

Internally, SSDictCursor uses mysql_use_result() which allows the server to start transferring the data before the acquiring is complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文