I am executing a query that is invalid, but I’d like to be able to see exactly what the query is being executed by pymysql to debug it. Is there a way to do something like:
try:
self.cursor.execute(SQL, tuple(PARAMS))
except:
print (self.cursor.last_executed_statement) # ??
Advertisement
Answer
As per the documentation, you should be able to do:
print (self.cursor.statement)
This read-only property returns the last executed statement as a string. The
statementproperty 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
statementproperty contains the entire statement string and theexecute()call returns an iterator that can be used to process results from the individual statements. Thestatementproperty for this iterator shows statement strings for the individual statements.