5

I'd like to print the code a particular session generates and sends to the DB. I've seen this question but it echoes all the SQL that SQLAlchemy generates. I want just the SQL generated on behalf of a particular session. Bonus points if it can be printed before it is actually send to the DB (like we can with the Query object)

I know I can print the SQL of a query, but in this situation

a = Table(id=1)
session.add(a)
b = Table(id=4)
session.add(a)
b.column = 5
session.commit()

there is no query to print, but the session is beginning a transaction, inserting rows and committing the transaction.

I'd like the session to print to stdout (or a file, if that's not possible) the SQL commands it's sending to the DB.

The (immaginary) SQL I expect to find in the console is

BEGIN TRANSACTION
INSERT INTO Table (id) VALUES (1);
INSERT INTO Table (id, column) VALUES (4,5);
COMMIT TRANSACTION

I fully understand that the lines might not all come together and might be interleaved by other code prints (the session is beginning the transaction first, then later the code trigger the flush of the insert, and as last thing the session issues the commit)

Is that possible?

Community
  • 1
  • 1
Makers_F
  • 3,033
  • 4
  • 36
  • 51
  • This looks like a duplicate. If it is not, just edit to make it clear why the linked question does not work for you and ping me here and I'll vote to re-open. – Sean Vieira Nov 09 '15 at 04:24
  • @SeanVieira The output shows everything that is sent to the DB, like table creation, SQL from other sessions, etc. I'd like to only see the SQL send because of a specific session. Moreover, I'd like to be able to see the SQL before actually sending it (like it can be done with queries). Does it make sense? – Makers_F Nov 09 '15 at 09:13
  • @SeanVieira Moreover for me echo=True doesn't outout anything to stdout – Makers_F Nov 09 '15 at 09:16
  • 1
    @SeanVieira Found out why it doesn't output anything. the logging module was not writing to console. If I change the logging module to print to console I can see the engine prints – Makers_F Dec 14 '15 at 02:22
  • @Makers_F Have you got answer? – Mangesh Sambare Oct 27 '17 at 14:10
  • @MangeshSambare I don't remember, but from my last comment I can imagine that after configuring the logging module to print to stdout I started getting the prints – Makers_F May 04 '18 at 00:12

0 Answers0