cursor.execute 的字符串未执行

String of cursor.execute without executing

我正在使用以下方法来使用 pymysql 参数化 SQL+args 查询但不执行它(至少合法地):

try:
    self.cursor.execute(self.sql + ' ***', self.sql_args or tuple())    
except pymysql.err.ProgrammingError:                                      
    self._sql_formatted = self.cursor._last_executed.rstrip('* ')

是否真的有 pymysql 有的方法只是格式化 SQL 字符串而不执行它,还是我需要使用类似上面的方法?或者,另一种选择:

self.cursor.execute('DESCRIBE ' + self.sql, self.sql_args or tuple())
self._sql_formatted = self.cursor._last_executed.replace('DESCRIBE ', '')

cursor.mogrify方法

Returns the exact string that is sent to the database by calling the execute() method.

>>> conn = pymysql.connect(host='localhost', user='root', password='password', database='test')
>>> cur = conn.cursor()
>>> stmt1 = """SELECT id, password FROM my_user WHERE name = %s;"""
>>> print(cur.mogrify(stmt1, ('Alice',)))
SELECT id, password FROM my_user WHERE name = 'Alice';