0

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.

Murali
  • 1,084
  • 1
  • 11
  • 28

1 Answers1

0

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'}
JonnyTieM
  • 177
  • 2
  • 12
  • I am in middle of development phase , I can't be able to go back to engine creation as you specified.What can I do now? – Murali Nov 16 '15 at 14:50
  • you're probably calling sqlalchemy.create_engine(...) somewhere in you're code. You could just adjust the parameters. Do you use a logger? Then you could try to use this: http://stackoverflow.com/questions/2950385/debugging-displaying-sql-command-sent-to-the-db-by-sqlalchemy (Look at the accepted answer.) – JonnyTieM Nov 16 '15 at 15:00
  • just enable logging using standard python logging module: `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. – van Nov 17 '15 at 12:25