如何在 Python 2.7 中获取 Oracle 数据库查询的字符串表示形式

How to get string representation of query for Oracle database in Python 2.7

我正在向服务器发送一些准备好的 SQL 语句和变量字典,以便使用 Python 2.7 进行替换,以便在 Oracle 数据库上远程执行。

我想记录将在服务器上执行的最终查询,但找不到获取最终查询的字符串表示形式的方法。

准备语句示例:

delete from my_table where col1=:date and col2=:string and col3=:num

词典示例:

{'date':datetime.datetime(2016, 3, 25, 0, 0), 'string':'abc', 'num':42}

我想得到什么(无论 SQL,可以用我的数据执行):

delete from my_table where col1=TO_DATE('25-03-2016 00:00','dd-mm-yyyy hh24:mi') and col2='abc' and col3=42

Why do you want this? Since that's not what gets executed, and you have to worry about quoting for it to be even realistic, it seems pointless. Just log the statement & the parameters if logging is the goal, that's the only way to reproduce the query accurately. – Mat Mar 28 at 16:07

感谢 Mat 的评论。