Get executed sql statement in pymysql
Question:
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) # ??
Answers:
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
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.
pep 0249 defines no way to do that. But pymysql has a mogrify
method that will show the generated SQL.
values={ 'qty': 10, 'descr': "chocolate cake" }
sql='''INSERT INTO mywishes
( quantity, description )
VALUES ( %(qty)s, %(descr)s )
'''
try:
cursor.execute( sql, values )
except pymysql.ProgrammingError:
print("Hum, there is an error in the sql...")
print(cursor.mogrify(sql, values))
Also note that if you don’t get that far, it means pymysql can’t map your SQL text and parameter to an actual SQL statement. This can happen for example if your param list is too long or too short. The following will raise a TypeError:
sql="insert into mywishes ( quantity, description ) values ( %s, %s )"
cursor.mogrify(sql, [1])
cursor.mogrify(sql, [1,2,3])
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) # ??
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
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 theexecute()
call returns an iterator that can be used to process results from the individual statements. Thestatement
property for this iterator shows statement strings for the individual statements.
pep 0249 defines no way to do that. But pymysql has a mogrify
method that will show the generated SQL.
values={ 'qty': 10, 'descr': "chocolate cake" }
sql='''INSERT INTO mywishes
( quantity, description )
VALUES ( %(qty)s, %(descr)s )
'''
try:
cursor.execute( sql, values )
except pymysql.ProgrammingError:
print("Hum, there is an error in the sql...")
print(cursor.mogrify(sql, values))
Also note that if you don’t get that far, it means pymysql can’t map your SQL text and parameter to an actual SQL statement. This can happen for example if your param list is too long or too short. The following will raise a TypeError:
sql="insert into mywishes ( quantity, description ) values ( %s, %s )"
cursor.mogrify(sql, [1])
cursor.mogrify(sql, [1,2,3])