How do I use num_rows() function in the MySQLDB API for Python?
I have this statement
cursor = connection.cursor()
query = "SELECT * fro开发者_Python百科m table"
cursor.execute(query)
res = cursor.fetchall()
cursor = connection.cursor()
query = "SELECT * from table"
cursor.execute(query)
print cursor.rowcount
According to the Python Database API Specification v2.0, the rowcount
attribute of the cursor
object 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 rowcount
attribute.
The num_rows()
function you are looking for does not exist in the MySQLdb
module. There is an internal module called _mysql
which has a result
class with a num_rows
method, but you shouldn't be using that - the very existence of _mysql
is considered an implementation detail.
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)
精彩评论