6

This question feels fiendishly simple but I haven't been able to find an answer.

I have an ORM query object, say

query_obj = session.query(Class1).join(Class2).filter(Class2.attr == 'state')

I can read it into a dataframe like so:

testdf = pd.read_sql(query_obj.statement, query_obj.session.bind)

But what I really want to do is use a traditional SQL query instead of the ORM:

with engine.connect() as connection:
    # Execute the query against the database
    results = connection.execute(query_obj)
    # Fetch all the results of the query
    fetchall = results.fetchall()
    # Build a DataFrame with the results
    dataframe = pd.DataFrame(fetchall)

Where query is a traditional SQL string. Now when I run this I get an error along the lines of "query_obj is not executable" Anyone know how to convert the ORM query to a traditional query? Also how does one get the columns in after getting the dataframe?

Context why I'm doing this: I've set up an ORM layer on top of my database and am using it to query data into a Pandas DataFrame. It works, but it's frequently maxing out my memory. I want to cut my in-memory overhead with some string folding (pass 3 outlined here: http://www.mobify.com/blog/sqlalchemy-memory-magic/). That requires (and correct me if I'm wrong here) not using the read_sql string and instead processing the query's return as raw tuples.

5
  • What is exactly your question? Just how to put the results of a fetchall into a dataframe? Further, what is not working about the code above? It seems fine to me. Commented Aug 5, 2015 at 13:13
  • 1
    For the column names, you can get that from fetchall.keys() Commented Aug 5, 2015 at 13:14
  • I made an edit to clarify my question. I'm just not clear on how to convert the ORM query_obj into a SQL string. Commented Aug 5, 2015 at 13:19
  • See the docs here: sqlalchemy.readthedocs.org/en/latest/faq/… Commented Aug 5, 2015 at 13:33
  • :) I see the answer. If you post I'll give you answer! Commented Aug 5, 2015 at 13:34

3 Answers 3

4

The long version is described in detail in the 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 this can be used in read_sql.

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

2 Comments

The link is no longer valid.
The new location seems to be here: docs.sqlalchemy.org/en/14/faq/….
2

this may be a later version of sqlalchemy since the post.

print(query)

outputs the query you can copy and paste back into your script.

Comments

1

Fiendishly simple indeed. Per Jori's link to the docs, it just query_obj.statement to get the SQL query. So my code is:

with engine.connect() as connection:
    # Execute the query against the database
    results = connection.execute(query_obj.statement)
    # Fetch all the results of the query
    fetchall = results.fetchall()
    # Build a DataFrame with the results
    dataframe = pd.DataFrame(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.