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?

6
  • 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. Commented Nov 9, 2015 at 4: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? Commented Nov 9, 2015 at 9:13
  • @SeanVieira Moreover for me echo=True doesn't outout anything to stdout Commented Nov 9, 2015 at 9: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 Commented Dec 14, 2015 at 2:22
  • @Makers_F Have you got answer? Commented Oct 27, 2017 at 14:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.