MySqlDb any way to see the query string

N

News123

Hi,

I'm using MYSQLdb


and have following code


db = MySQLdb.connect(**cfg)
c = db.cursor()
qrystr = "insert mytable set id = %s , other_field = %s"
c.execute(qrystr, (id_val,other_field_val) )


What I wondered is whether there is any way to print the 'filled in'
query string for debuggin.

The reason I'm askng is, that I'd like to copy paste the 'filled in'
query string
and try it from the command line.

I know I could create it myself, but I had to do all the esaping myself.

Thanks for suggestions


N
 
D

Dennis Lee Bieber

Hi,

I'm using MYSQLdb
said:
What I wondered is whether there is any way to print the 'filled in'
query string for debuggin.
Just edit the MySQLdb cursors module -- it's plain Python unless
something has changed in the last year... Find the .execute() method,
and stuff in a debug print statement where it does the escaping and
"fill in" (reason MySQLdb uses %s placeholder is that it uses Python to
do the fill in; things may change if the module is ever updated to use
ver 5 prepared queries).

Come to think of it, one could probably duplicate the .execute()
method, creating a "d_execute" with the print statement, while not
affecting operational code... That way one wouldn't have to edit the
module just for testing.

(If creating a debug execute, one would also need to create a debug
executemany as it normally invokes the normal execute)

Something like (line 153 in my cursors.py):
-=-=-=-=-=-
if args is not None:
query = query % db.literal(args)
print repr(query) #debug output of final query <<<<====
try:
r = self._query(query)
except TypeError, m:
-=-=-=-=-=-

Or just preprocess the query in your code using

finalq = querysql % yourConnection.literal(argumenttuple)
 
M

MRAB

Dennis said:
Just edit the MySQLdb cursors module -- it's plain Python unless
something has changed in the last year... Find the .execute() method,
and stuff in a debug print statement where it does the escaping and
"fill in" (reason MySQLdb uses %s placeholder is that it uses Python to
do the fill in; things may change if the module is ever updated to use
ver 5 prepared queries).

Come to think of it, one could probably duplicate the .execute()
method, creating a "d_execute" with the print statement, while not
affecting operational code... That way one wouldn't have to edit the
module just for testing.
Why duplicate the method? Why not add a keyword argument to turn on
printing or a provide a file-type instance to which it should log the
query?
 
N

News123

Hi everybody,


im said:
You have the source code in front of you. After you do the execute, the
actual query string that was transmitted is available in "c._executed".

If you need to know the result before you submit it, you can scan the
source for the "execute" method and see how they do the quoting. It's not
that complicated.

Thanks for all of your answers.

In my case c._executed is absolutely sufficient.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top