Print the actual query MySQLdb runs?

xitrium picture xitrium · Aug 15, 2011 · Viewed 55.7k times · Source

I'm looking for a way to debug queries as they are executed and I was wondering if there is a way to have MySQLdb print out the actual query that it runs, after it has finished inserting the parameters and all that? From the documentation, it seems as if there is supposed to be a Cursor.info() call that will give information about the last query run, but this does not exist on my version (1.2.2).

This seems like an obvious question, but for all my searching I haven't been able to find the answer. Thanks in advance.

Answer

xitrium picture xitrium · Aug 25, 2011

We found an attribute on the cursor object called cursor._last_executed that holds the last query string to run even when an exception occurs. This was easier and better for us in production than using profiling all the time or MySQL query logging as both of those have a performance impact and involve more code or more correlating separate log files, etc.

Hate to answer my own question but this is working better for us.