MishaVacic
MishaVacic

Reputation: 1897

MySQLdb fetcall,AttributeError: 'int' object has no attribute 'fetchall'

I want to check if I have managed to import my csv file into MySQL db in proper manner.My code

import MySQLdb

mydb = MySQLdb.connect(host = 'localhost',user = 'milenko',passwd = 'nuklear',db = 'mm')
cur = mydb.cursor()
command = cur.execute('SELECT * FROM jul')
results = command.fetchall()

print (results)

But I got this

File "b12.py", line 6, in <module>
    results = command.fetchall()
AttributeError: 'int' object has no attribute 'fetchall'

I have seen previous SO posts,where people claim that number objects do not have fetcall object.I have copied this code from Python for MySQL from Albert Lukaszewski. How to pull down db content in one go?

Upvotes: 5

Views: 12120

Answers (1)

CodeP
CodeP

Reputation: 254

You can't call fetchall() on the result of a cursor.execute(), in fact, according to MySQLdb documentation, cursor.execute() return the number of affected rows by the query executed. To retrieve data you have to access to cursor results directly:

cur = mydb.cursor()
cur.execute('SELECT * FROM jul')
results = cur.fetchall()

Upvotes: 21

Related Questions