1

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'

2 Answers 2

3

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.

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

2 Comments

AttributeError: 'Connection' object has no attribute 'cursor' I am using sqlalchemy to connect to my database.
Then why are you trying to use fetchall? That's a method of the db api, not of sqlalchemy. As the sqlalchemy docs show, to get results from that you just iterate over the return value of execute.
-1
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()

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.