How to see the real SQL query in Python cursor.execute using pyodbc and MS-Access
It differs by driver. Here are two examples:
import MySQLdb
mc = MySQLdb.connect()
r = mc.cursor()
r.execute('select %s, %s', ("foo", 2))
r._executed
"select 'foo', 2"
import psycopg2
pc = psycopg2.connect()
r = pc.cursor()
r.execute('select %s, %s', ('foo', 2))
r.query
"select E'foo', 2"
The answer is : NO. I posted my question on the project's home Google Code (and in the Google Group) and the answer is:
Comment #1 on issue 163 by [email protected]: cursor.mogrify return query string http://code.google.com/p/pyodbc/issues/detail?id=163
For reference here is a link to the pyscopg documentation of their "mogrify" cursor method that the reporter is referring to: http://initd.org/psycopg/docs/cursor.html#cursor.mogrify
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.
You can use print cursor._last_executed
to get the last executed query.
Read in this answer that you can also use print cursor.mogrify(query,list)
to see the full query before or after executing.