Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the actual query MySQLdb runs?

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.

like image 906
xitrium Avatar asked Aug 15 '11 21:08

xitrium


People also ask

How do you print a query in Python?

You can use a print('your query here') in the python script itself. As you're fetching the data from your query / sql snippet inside some variable in you python script, you can use a dbms_output. put_line('you query here') in the query / . sql file and that should do the trick.

What is MySQLdb in Python?

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and is built on top of the MySQL C API. Packages to Install mysql-connector-python mysql-python.

What does MySQL Fetchall return?

fetchall() Method. The method fetches all (or all remaining) rows of a query result set and returns a list of tuples. If no more rows are available, it returns an empty list. You must fetch all rows for the current query before executing new statements using the same connection.


1 Answers

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.

like image 198
xitrium Avatar answered Oct 02 '22 07:10

xitrium