18

I have this statement

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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

4

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)

1 Comment

This worked for me. Thank you @oğuzhan-karabulut

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.