Re: [pysqlite] [APSW 3.6.10-r1] Displaying query before update?

Roger Binns <[email protected]> Sat, 21 Mar 2009 10:50:26 -0700
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
Gilles wrote:
> try:
> 	#How to only display the query, not run it directly?
> 	cursor.execute(sql, (name,address,zip,id) )

How about:

         print sql, (name,address,zip,id)

Note that when SQLite uses bindings they aren't formatted into the
string.  ie if you execute("select ?", (3,)) then SQLite does *not*
convert it to execute("select 3").

APSW includes builtin hooks to do SQL and row tracing.  See
http://apsw.googlecode.com/svn/publish/execution.html#tracing

Even easier you can use apswtrace which will show you what is going on
without modifying your code at all.  See
http://apsw.googlecode.com/svn/publish/execution.html#apsw-trace

If you want the ability to reject certain SQL statements before they are
executed then you can use the SQLite authorization functionality -
http://apsw.googlecode.com/svn/publish/connection.html#apsw.Connection.setauthorizer

Finally if you only have a single SQL statement to run (an UPDATE in
this case) then there is no need to wrap it in BEGIN/COMMIT as SQLite
will do that automatically.  You only need BEGIN/COMMIT if using more
than one SQL statement to define where the transaction boundaries are.

You can also use the Connection as a context manager ("with" statement)
which may be easier to read code.  See
http://apsw.googlecode.com/svn/publish/connection.html#apsw.Connection.__enter__

Roger