如果不同的游标,MySQLdb.cursors.Cursor.execute返回不同​​的值,为什么? [英] MySQLdb.cursors.Cursor.execute returns different values in case of different cursors why?

查看:247
本文介绍了如果不同的游标,MySQLdb.cursors.Cursor.execute返回不同​​的值,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见这两个python代码段,

See these two python code snippets,

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行.

Why the returned number of rows are different in above two cases ? And just FYI there are 1147 rows in a table.

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

SSCursor is used for saving result at servers side. Is it the reason ? What all rows are affected by this select query ?

有人知道吗?

推荐答案

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

The standard cursor used by MySQLdb is a stored result cursor. This means that the entire result set is transferred from the server and cached in the client's memory as a result of the execute() call.

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

A SSCursor is a server side cursor will only return results to the client as they are requested.

游标execute()调用将返回MySql C API函数 mysql_affected_rows(),它依次返回

The cursor execute() call will return the result of the MySql C API function mysql_affected_rows() which in turn returns the result of mysql_num_rows() for SELECT queries.

由于结果存储在服务器端,因此客户端在迭代结果之前不知道影响了多少行.

Because results are stored server side, the client does not know how many rows have been affected until it iterates over the results.

mysql_num_rows()的文档说:

mysql_num_rows()的使用取决于您是否使用 mysql_store_result()或mysql_use_result()返回结果集. 如果您使用mysql_store_result(),则可能会调用mysql_num_rows() 立即地.如果您使用mysql_use_result(),则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()调用的结果.

If you want to obtain a count of the number of rows, use SELECT COUNT(*) from geo_weathers rather than rely on the result of the execute() call.

这篇关于如果不同的游标,MySQLdb.cursors.Cursor.execute返回不同​​的值,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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