开发者

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

开发者 https://www.devze.com 2023-01-19 01:18 出处:网络
I have this statement cursor = connection.cursor() query = \"SELECT * fro开发者_Python百科m table\"

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号