Tanu
Tanu

Reputation: 267

How to get the raw SQL of the query being executed by SQL Alchemy

we are using SQL alchemy as the ORM for our project. i am trying to get & analyze the raw SQL that gets generated (via SQL alchemy) for performance purposes. can somebody please let me know if there is a way to gather the raw SQL before it gets committed to the database.

Upvotes: 4

Views: 2502

Answers (1)

Alex Smith
Alex Smith

Reputation: 1535

You can set echo=True when instantiating your engine:

>>> from sqlalchemy import create_engine
>>> engine = create_engine("mysql://localhost/mydb", echo=True)

This causes all SQL statements to be logged via normal Python logging. You can easily capture these messages to a file by using the appropriate log configuration, as shown in these Python logging examples.

Reference: SQLAlchemy Engine Configuration

Upvotes: 3

Related Questions