python - 获取pymysql中执行的sql语句

标签 python mysql sql

我正在执行无效的查询,但我希望能够准确查看 pymysql 正在执行的查询内容来调试它。有没有办法做这样的事情:

try:
    self.cursor.execute(SQL, tuple(PARAMS))
except:
    print (self.cursor.last_executed_statement) # ??

最佳答案

根据the documentation ,你应该能够做到:

print (self.cursor.statement)

This read-only property returns the last executed statement as a string. The statement property can be useful for debugging and displaying what was sent to the MySQL server.

The string can contain multiple statements if a multiple-statement string was executed. This occurs for execute() with multi=True. In this case, the statement property contains the entire statement string and the execute() call returns an iterator that can be used to process results from the individual statements. The statement property for this iterator shows statement strings for the individual statements.

关于python - 获取pymysql中执行的sql语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59205041/

相关文章:

python - ValueError : You are trying to merge on datetime64[ns, UTC] 和对象列

mysql:内部查询在 selectif 语句中不起作用

sql - SQL Server 中按两列计数?

mysql - 用案例更新表

python - Pandas分类数据: Storing the transformation

Python 多处理 : no performance gain with multiple processes

python - 无法在 .select() 方法中使用多个属性

PHP LoginScript MySQL 检查结果错误

c# - 将属性添加到关联

sql - 透视 - SQL - 来自子查询的值