DBI trace?

Gerhard Häring gh at ghaering.de
Mon Nov 24 04:16:12 EST 2003


Andrew Fabbro wrote:
> Is there something akin to perl's  DBI->trace in python? [...]

I suppose you mean logging of SQL statements. There is no standard way 
in the Python DB-API, I can only speak for the projects I'm involved in:

pyPgSQL: There is a hidden attribute 'toggleShowQuery' in the low-level 
connection object, which will log the queries to stderr (stderr is 
currently hard-coded):

#v+
gerhard ( at ) gargamel:~$ python
Python 2.2.2 (#1, Nov 30 2002, 23:19:58)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.
 >>> from pyPgSQL import PgSQL
 >>> con = PgSQL.connect()
 >>> con.conn.toggleShowQuery
'On'
 >>> cursor = con.cursor()
QUERY: BEGIN WORK
 >>> cursor.execute("select * from test")
QUERY: DECLARE "PgSQL_0811F1EC" CURSOR FOR select * from test
QUERY: FETCH 1 FROM "PgSQL_0811F1EC"
QUERY: SELECT typname, -1 , typelem FROM pg_type WHERE oid = 23
QUERY: SELECT typname, -1 , typelem FROM pg_type WHERE oid = 1043
 >>> result = cursor.fetchmany(5)
QUERY: FETCH 4 FROM "PgSQL_0811F1EC"
 >>> result
[[None, 'A'], [None, 'B'], [None, 'C'], [None, 'F'], [None, 'F']]
 >>> con.commit()
QUERY: CLOSE PgSQL_0811F1EC
QUERY: COMMIT WORK
#v-

PySQLite:

In the .connect() call, there is an optional parameter command_logfile, 
which will accept a file-like object (or anything that implements a 
write() method).

HTH,

-- Gerhard






More information about the Python-list mailing list