0

If I write,

cursor=db.cursor(pymysql.cursors.DictCursor)
cursor.execute(query)
result=cursor.fetchall()
print(result)

this works and print results as a dictionary.

But if I write like this,

cursor=db.cursor(pymysql.cursors.DictCursor)
result=cursor.execute(query).fetchall()
print(result)

it gives me error:

Traceback (most recent call last):
  File "senti4SDanalyze.py", line 22, in <module>
    constructMaleResults()
  File "senti4SDanalyze.py", line 12, in constructMaleResults
    result=cursor.execute(query).fetchall()
AttributeError: 'int' object has no attribute 'fetchall'

why is that?

1
  • Because cursor.execute(query) returns an int. Commented Jun 22, 2018 at 2:19

1 Answer 1

2

In result=cursor.fetchall() the method fetchall is being called on the object cursor.

In result=cursor.execute(query).fetchall() the method fetchall is being called on the object (or native type) returned by the execute method.

To understand this better, explore with the built-ins type and dir:

type(cursor)
dir(cursor)
type(cursor.execute(query))
dir(cursor.execute(query))
Sign up to request clarification or add additional context in comments.

Comments

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.