如何在 MySQLDB API for Python 中使用 num_rows() 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3890517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:13:09  来源:igfitidea点击:

How do I use num_rows() function in the MySQLDB API for Python?

pythonmysql

提问by Bingimar

I have this statement

我有这个声明

cursor = connection.cursor()
query = "SELECT * from table"
cursor.execute(query)
res = cursor.fetchall()

采纳答案by Tamás

cursor = connection.cursor()
query = "SELECT * from table"
cursor.execute(query)
print cursor.rowcount

According to the Python Database API Specification v2.0, the rowcountattribute of the cursorobject should return the number of rows that the last query produced or affected (the latter is for queries that alter the database). If your database module conforms to the API specification, you should be able to use the rowcountattribute.

根据Python 数据库 API 规范 v2.0,对象的rowcount属性cursor应返回上次查询产生或影响的行数(后者用于更改数据库的查询)。如果您的数据库模块符合 API 规范,您应该能够使用该rowcount属性。

The num_rows()function you are looking for does not exist in the MySQLdbmodule. There is an internal module called _mysqlwhich has a resultclass with a num_rowsmethod, but you shouldn't be using that - the very existence of _mysqlis considered an implementation detail.

模块num_rows()中不存在您要查找的函数MySQLdb。有一个内部模块被调用_mysql,它有一个result带有num_rows方法的类,但你不应该使用它 - 的存在_mysql被认为是一个实现细节。

回答by o?uzhan karabulut

Most voted answer is not working yet. It should be like that:

大多数投票的答案还没有工作。应该是这样的:

cursor = connection.cursor()
query = "SELECT * FROM table"
cursor.execute(query)
cursor.fetchall()
print (cursor.rowcount)