Convert sqlalchemy ORM query object to sql query for Pandas DataFrame -
this question feels fiendishly simple haven't been able find answer.
i have orm query object, say
query_obj = session.query(class1).join(class2).filter(class2.attr == 'state')
i can read dataframe so:
testdf = pd.read_sql(query_obj.statement, query_obj.session.bind)
but want use traditional sql query instead of orm:
with engine.connect() connection: # execute query against database results = connection.execute(query_obj) # fetch results of query fetchall = results.fetchall() # build dataframe results dataframe = pd.dataframe(fetchall)
where query traditional sql string. when run error along lines of "query_obj not executable" know how convert orm query traditional query? how 1 columns in after getting dataframe?
context why i'm doing this: i've set orm layer on top of database , using query data pandas dataframe
. works, it's maxing out memory. want cut in-memory overhead string folding (pass 3 outlined here: http://www.mobify.com/blog/sqlalchemy-memory-magic/). requires (and correct me if i'm wrong here) not using read_sql string , instead processing query's return raw tuples.
the long version described in detail in faq of sqlalchemy: http://sqlalchemy.readthedocs.org/en/latest/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined
the short version is:
statement = query.statement print(statement.compile(engine))
the result of can used in read_sql
.
Comments
Post a Comment