I need to know the way of getting the SQLAlchemy query printed with values.
I can be able to print the query which is getting processed, But it is not showing the values instead it just showing the field name itself.
SELECT recordsets.id AS recordsets_id, recordsets.version AS recordsets_version
FROM recordsets
WHERE recordsets.domain_id = :domain_id_1
AND recordsets.tenant_id IN (:tenant_id_1, :tenant_id_2)
AND recordsets.name IN (:name_1, :name_2)
So instead of :name_1, :name_2 or :tenant_id_1, :tenant_id_2 I need to see the values itself.
1 Answer 1
When you're creating your engine you have the possibility to turn on logging, by setting echo on True. That will print all queries with their parameters.
Example:
engine = create_engine("mysql://scott:tiger@hostname/dbname",
encoding='latin1', echo=True)
See: http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#engine-creation-api
For your example the output will look like:
2015年11月16日 15:50:25,137 INFO sqlalchemy.engine.base.Engine SELECT recordsets.id AS recordsets_id, recordsets.version AS recordsets_version
FROM recordsets
WHERE recordsets.domain_id = :domain_id_1
AND recordsets.tenant_id IN (:tenant_id_1, :tenant_id_2)
AND recordsets.name IN (:name_1, :name_2)
2015年11月16日 15:50:25,137 INFO sqlalchemy.engine.base.Engine {'domain_id_1': 'THE actual domain id', 'tenant_id_1': 234, 'tenant_id_2': 456, 'name_1': 'your first name', 'name_2': 'your second example name'}
3 Comments
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) to enable logging any later time in your code after engine has been created. Here I assume that you configured logging at the start of your application already.