Retrieving Raw, Compiled SQL Queries from SQLAlchemy Expressions
SQLAlchemy's query interface provides a powerful abstraction for constructing database queries. However, accessing the underlying SQL statement can be challenging. To obtain the raw, compiled SQL query with bound parameters, consider the following options:
using the literal_binds argument
The documentation suggests using literal_binds to print a query q with parameters:
print(q.statement.compile(compile_kwargs={"literal_binds": True}))
Caveats
Documentation Warning
The documentation warns that you should not use this method with data from untrusted sources such as web forms. SQLAlchemy's mechanisms for converting Python values to SQL string values do not securely handle untrusted data and do not check the type of data being passed.
The above is the detailed content of How Can I Retrieve the Raw, Compiled SQL Query from a SQLAlchemy Expression?. For more information, please follow other related articles on the PHP Chinese website!