在pymysq中获取执行的sql语句

2024-06-17 09:04:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在执行一个无效的查询,但我希望能够准确地看到pymysql正在执行的查询,以便对其进行调试。有没有一种方法可以做到:

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

Tags: 方法selfexecutesqlparamscursorstatementlast
1条回答
网友
1楼 · 发布于 2024-06-17 09:04:32

根据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.

相关问题 更多 >