How can I log queries in Sqlite3 with Python?

logging, python, sqlite

Solution

Python 3.3 has sqlite3.Connection.set_trace_callback:

import sqlite3
connection = sqlite3.connect(':memory:')
connection.set_trace_callback(print)

The function you provide as argument gets called for every SQL statement that is executed through that particular Connection object. Instead of `print`, you may want to use a function from the `logging` module.

Problem

I'm using Sqlite3 database in my Python application and query it using parameters substitution. For example: ``` cursor.execute('SELECT * FROM table WHERE id > ?', (10,)) ``` Some queries do not return results properly and I would like to log them and try to query sqlite manually. How can I log these queries with parameters instead of question marks?

Original source

Related problems