gpt4 book ai didi

python - MySQLdb.cursors.Cursor.execute 在不同游标的情况下返回不同的值,为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:55 27 4
gpt4 key购买 nike

看看这两个 python 代码片段,

conn = MySQLdb.connect(c['host'], c['user'], c['password'], c['db'])
cur = conn.cursor()
cur.execute("select * from geo_weathers;) -> **1147L**

conn = MySQLdb.connect(c['host'], c['user'], c['password'], c['db'], cursorclass=MySQLdb.cursors.SSCursor)
cur = conn.cursor()
cur.execute("select * from geo_weathers") -> **18446744073709551615L**

为什么以上两种情况返回的行数不同?仅供引用,表中有 1147 行。

SSCursor 用于在服务器端保存结果。是这个原因吗?此选择查询影响所有行的哪些内容?

有人知道吗?

最佳答案

MySQLdb 使用的标准游标是存储结果游标。这意味着作为 execute() 调用的结果,整个结果集从服务器传输并缓存在客户端的内存中。

SSCursor 是服务器端游标,只会在请求时将结果返回给客户端。

游标execute()调用将返回MySql C API函数的结果mysql_affected_rows()依次返回 mysql_num_rows() 的结果对于 SELECT 查询。

因为结果存储在服务器端,客户端在迭代结果之前不知道有多少行受到影响。

mysql_num_rows() 的文档是这样说的:

The use of mysql_num_rows() depends on whether you use mysql_store_result() or mysql_use_result() to return the result set. If you use mysql_store_result(), mysql_num_rows() may be called immediately. If you use mysql_use_result(), mysql_num_rows() does not return the correct value until all the rows in the result set have been retrieved.

如果您想获得行数的计数,请使用 SELECT COUNT(*) from geo_weathers 而不是依赖 execute() 调用的结果.

关于python - MySQLdb.cursors.Cursor.execute 在不同游标的情况下返回不同的值,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com