From: News123 on
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
From: MRAB on
Dennis Lee Bieber wrote:
> On Sat, 10 Jul 2010 20:22:21 +0200, News123 <news1234(a)free.fr> declaimed
> the following in gmane.comp.python.general:
>
>> Hi,
>>
>> I'm using MYSQLdb
> <snip>
>> 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.
>
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?
From: Tim Roberts on
News123 <news1234(a)free.fr> wrote:
>
>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.

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.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: News123 on
Hi everybody,


im Roberts wrote:
> News123 <news1234(a)free.fr> wrote:
>> 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.
>
> 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.