Last active
December 25, 2019 14:51
-
-
Save kwatch/4672421 to your computer and use it in GitHub Desktop.
How to convert query object into non-prepared sql string in SQLAlchemy and psycopg2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query2sql(query): | |
"""convert query object into non-prepared sql string in SQLAlchemy and psycopg2""" | |
compiler = query.statement.compile() | |
params = compiler.params | |
prepared_sql = compiler.string # or str(compiler) | |
psycopg2_cursor = query.session.connection().connection.cursor() | |
sql = psycopg2_cursor.mogrify(prepared_sql, params) | |
return sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another solution: http://stackoverflow.com/questions/4617291/how-do-i-get-a-raw-compiled-sql-query-from-a-sqlalchemy-expression/4617623#4617623