Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log queries in Sqlite3 with Python?

Tags:

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?

like image 729
gennad Avatar asked Aug 04 '11 13:08

gennad


People also ask

Which of the following function are used to execute the query in sqlite3 Python?

The cursor() function is used to assist with executing our SQL queries.


1 Answers

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.

like image 161
Marcel M Avatar answered Oct 14 '22 13:10

Marcel M