Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get prepared query which is sent to db

When using database libraries like pyodbc that implement the Python Database API Specification how can you get the fully prepared query after parameter substitution has been applied. I'm making a call to a Sybase stored procedure that will receive 18 arguments through parameter substitution. I would like to capture the actual call that was made and log it to help in debugging. A simpler example of what I need:

pyodbc example

import pyodbc
conn = pyodbc.connect('DSN=test;PWD=password')
c = conn.cursor()
c.execute('CREATE TABLE stocks (symbol varchar(10), price real)')
c.execute("INSERT INTO stocks VALUES (?, ?)", ('RHAT', 35.14))
c.execute('SELECT * FROM stocks WHERE symbol=?', ('RHAT',))
print c.fetchone()

expected final queries (to be logged)

CREATE TABLE stocks (symbol varchar(10), price real)
INSERT INTO stocks VALUES ('RHAT', 35.14)
SELECT * FROM stocks WHERE symbol = 'RHAT'

sqlite3 example

import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('CREATE TABLE stocks (symbol text, price real)')
c.execute("INSERT INTO stocks VALUES (?, ?)", ('RHAT', 35.14))
c.execute('SELECT * FROM stocks WHERE symbol=?', ('RHAT',))
print c.fetchone()

I've put the sqlite3 example as it doesn't need pyodbc to try it out.

UPDATE

It seems that when one uses prepared statements the process of filling the data into the template is done server side in the DBMS. So frequently there is no method or api to get that final version of the query from the server, as described in user581592's answer. Other links of note are Issue 163 on pyodbc that discusses this further. Also some db libraries like psycopg have added a method mogrify that will return the final statement. But as mentioned in their documentation this is not part of the DB API.

like image 796
Marwan Alsabbagh Avatar asked Jul 11 '13 10:07

Marwan Alsabbagh


1 Answers

From https://groups.google.com/forum/#!topic/pyodbc/656mRdbjFuc

pyodbc does not modify the SQL in any way. In general, "good" ODBC drivers don't either.

Since it is not a supported function, there is no ODBC function for retrieving the final SQL, so there is no way pyodbc could offer this.

So, your best bet might be to log the queries, and arguments separately in your script, or you can try to configure some level of query logging on the database, if it is feasible.

like image 132
user581592 Avatar answered Sep 28 '22 02:09

user581592