philnext philnext - 1 year ago 117
Python Question

How to see the real SQL query in Python cursor.execute

I use the following code in Python (with pyodbc for a MS-Access base).

cursor.execute("select a from tbl where b=? and c=?", (x, y))

It's Ok but, for maintenance purposes, I need to know the complete and exact SQL string send to the database.

Is it possible and how ?

Answer Source

The answer is : NO. I post my question on the project' home Google Code (and in the Google Group) is the answer is :

Comment #1 on issue 163 by cursor.mogrify return query string

For reference here is a link to the pyscopg documentation of their "mogrify" cursor method that the reporter is referring to:

pyodbc does not perform any such translations of the SQL: it passes parameterized SQL straight through to the ODBC driver verbatim. The only processing involved is translating parameters from Python objects to C types supported by the ODBC API.

Some transformation on the SQL may be performed in the ODBC driver before it is sent to the server (eg Microsoft SQL Native Client does this) but these transformations are hidden from pyodbc.

Consequently I think it is not feasible to provide a mogrify function in pyodbc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download