print executed query

Fred C fred at bsdhost.net
Tue May 4 01:02:47 EDT 2010


On Apr 29, 2010, at 9:49 AM, Philip Semanchuk wrote:

> 
> On Apr 29, 2010, at 12:01 PM, someone wrote:
> 
>> Hello!
>> 
>> Is there a way to print a query for logging purpose as it was or will
>> be sent to database, if I don't escape values of query by myself?
>> 
>> cursor.execute(query, [id, somestring])
>> 
>> I could print query and values separate, but it would be great, if I
>> could see how query is constructed and can the also copy it and
>> execute in console.
>> 
>> Im using psycopg2, btw
> 
> 
> If you can fiddle with the Postgres server settings, the server has options for logging lots of things, including the queries it executes.
> 


Yes you can fiddle like that:

>>> cur.mogrify("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar'))
"INSERT INTO test (num, data) VALUES (42, E'bar')"

-fred-



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3676 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20100503/26f987ec/attachment.bin>


More information about the Python-list mailing list