How to get prepared query which is sent to db

泄露秘密 提交于 2019-12-10 13:32:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17591459/how-to-get-prepared-query-which-is-sent-to-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!