AttributeError: 'NoneType' object has no attribute 'cursor' – Getting this error when trying to use pd.read_sql() with flask-sqlalchemy query

Question:

I have a flask-sqlalchemy query. I know that the below flask-sqlalchemy query code is valid because I am able to populate data on another page in an html table with the ‘eintest’ variable.

ticker = 'msft'
eintest = db.session.query(company).filter(company.instance==ticker)

All I am trying to do is convert that flask-sqlalchemy code above into a pandas data frame, as I am trying to do below.

df = pd.read_sql(eintest.statement, db.session.bind)

When I run my flask app, I get the error, "AttributeError: ‘NoneType’ object has no attribute ‘cursor’". Does anyone know why this is? I’ve been trying many variations of that one line to read sql into a pandas dataframe but no luck yet.

Asked By: JamesN

||

Answers:

Arguments for pandas read_sql is a sql statement and a connection. You are passing a statement and a session. Here’s what you’ll need to change to get it to work:

# Load to a pandas dataframe
df = pd.read_sql(eintest.statement, db.get_engine())
Answered By: Yaakov Bressler
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.