Python SQLite如何获取正在执行的SQL字符串语句 [英] Python SQLite how to get SQL string statement being executed

查看:12
本文介绍了Python SQLite如何获取正在执行的SQL字符串语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个 SQL 语句,它只需要在对数据库执行之前使用参数完成.例如:

Let's say we have a SQL statement that just needs to be completed with the parameters before getting executed against the DB. For instance:

sql = '''
      SELECT  id, price, date_out
      FROM sold_items
      WHERE date_out BETWEEN ? AND ?
      '''

database_cursor.execute(sql, (start_date, end_date))

如何获取解析并执行的字符串?,类似这样:

How do I get the string that is parsed and executed?, something like this:

SELECT  id, price, date_out
FROM sold_items
WHERE date_out BETWEEN 2010-12-05 AND 2011-12-01

在这个简单的例子中它不是很重要,但我有其他更复杂的 SQL 语句,出于调试目的,我想在我的 SQLite 管理器中自己执行它们并检查结果.

In this simple case it's not very important, but I have other SQL Statements much more complicated, and for debugging purposes I would like to execute them myself in my sqlite manager and check the results.

提前致谢

推荐答案

UPDATE.我从 这个网页 了解到,自从 Python 3.3您可以使用

UPDATE. I learned from this web page that since Python 3.3 you can trigger printing of executed SQL with

connection.set_trace_callback(print)

如果您想恢复到静默处理,请使用

Should you want to revert to silent processing, use

connection.set_trace_callback(None)

您可以使用其他函数代替print.

You can use another function instead of print.

这篇关于Python SQLite如何获取正在执行的SQL字符串语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆