MySQLdb - parameterised SQL - how to see resulting SQL ?

S

shearichard

Hi - I've got SQL that looks like this ...

cursor =
self.MySQLDb_conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
sqlQuery = "SELECT * FROM T1 WHERE C1 = %s and C2 = %s"
sql = cursor.execute(sqlQuery,(strURLAlias,strSessionID))
rows = cursor.fetchall()
cursor.close

.... i would be interested in seeing what the actual SQL used by the
..execute looks like after the replacements have been done. Is there a
way of doing this ?

thanks

richard.
 
D

Dan Sommers

On 17 May 2006 18:41:55 -0700,
Hi - I've got SQL that looks like this ...
cursor =
self.MySQLDb_conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
sqlQuery = "SELECT * FROM T1 WHERE C1 = %s and C2 = %s"
sql = cursor.execute(sqlQuery,(strURLAlias,strSessionID))
rows = cursor.fetchall()
cursor.close

ITYM cursor.close()
... i would be interested in seeing what the actual SQL used by the
.execute looks like after the replacements have been done. Is there a
way of doing this ?

Taking a quick peek at pysqlite and the API spec, I'd say no, not a
standard way.

OTOH, eventually, mysqldb has to create that SQL in order to pass it to
the database for execution, so it's probably as simple as finding the
right place to put a "print" statement and/or to store the finished SQL
in the cursor object.

HTH,
Dan
 
B

Ben Finney

... i would be interested in seeing what the actual SQL used by the
.execute looks like after the replacements have been done. Is there a
way of doing this ?

I also think this would be a very useful feature for enabling logging,
debugging, and other introspection. It surely is possible for the code
to present that information (it must construct the final SQL statement
to pass to the database engine), but I can't see a way to get at it
with the current DB API.
 
D

Damjan

... i would be interested in seeing what the actual SQL used by the
.execute looks like after the replacements have been done. Is there a
way of doing this ?

On my development machine I'm using the "--log" argument to MySQL so that it
logs all queries and connections. I wouldn't recommend that on a production
machine though.

The logfile is stored in /var/lib/mysql/<hostname>.log
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top