Python 中的 SQL:如何调试/打印带有变量 (%s) 的 SQL 查询

and*_*opr 5 mysql-connector python-3.7

我正在使用 mysql.connector 和 Python 3.7。

对于 SQL 查询,我使用以下方法:

cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))

问题是......当我想打印或调试整个 sql 查询时,我无法获取值而不是 %s (我知道重点是你不能)。

那么你们如何调试这个呢?

谢谢。

Pau*_*ulS 3

打印格式化字符串的语法如下:

name = "John"
print("Hello, %s!" % name)
Run Code Online (Sandbox Code Playgroud)

您可以将其应用到 SQL 语句并按如下方式打印:

print("INSERT INTO table VALUES (%s, %s, %s)" % (var1, var2, var3))
Run Code Online (Sandbox Code Playgroud)