r0xette
r0xette

Reputation: 908

AttributeError: 'Connection' object has no attribute 'fetchall'

What am I doing wrong here?

engine_str = 'mysql+mysqlconnector://my_username:my_pass@localhost/my_db'
engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
connection = engine.connect()
query = "SELECT * from history_table"

connection.execute(query)
rows = connection.fetchall()

Error

AttributeError: 'Connection' object has no attribute 'fetchall'

Upvotes: 1

Views: 5580

Answers (2)

venkatadileep
venkatadileep

Reputation: 183

engine_str = 'mysql+mysqlconnector://my_username:my_pass@localhost/my_db'
engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
connection = engine.connect()
query = "SELECT * from history_table"

rows = connection.execute(query) 
# cursor will save records later we can fetch but in sqlalchemy 
# have to save the results while executing 

result = rows.fetchall()

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

fetchall is a method of a cursor, not a connection.

query = "SELECT * from history_table"
cursor = connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()

I have no idea why you've brought sqlalchemy into this though. You are not using it at all; you are just going straight to the underlying database API.

Upvotes: 3

Related Questions