Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQLAlchemy how do I preview SQL statements before committing for debugging purposes?

I want to see the SQL code instead of doing an actual db.commit(). This is for a one-off database population script that I want to verify is working as intended before actually making the changes.

like image 365
wuxiekeji Avatar asked Apr 11 '26 07:04

wuxiekeji


1 Answers

Try calling str() on the query object.

print query_object.str()

From:

How do I get a raw, compiled SQL query from a SQLAlchemy expression?

Other possible solutions:

  • SQLAlchemy: print the actual query
  • How to retrieve executed SQL code from SQLAlchemy

The newest (as of v0.9) answer is also:

  • Retrieving ultimate sql query sentence (with the values in place of any '?') (by Mike Bayer)
like image 162
chishaku Avatar answered Apr 13 '26 22:04

chishaku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!