1

I am using the following approach to using pymysql to parametrize the SQL+args query but not execute it (legitimately, at least):

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

Is there actually a method that pymysql has where it just formats the SQL string without executing it, or do I need to use something like the above? Or, another option:

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

1 Answer 1

1

The cursor.mogrify method

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';
Sign up to request clarification or add additional context in comments.

1 Comment

I'd guess that it's a back-formation from "transmogrify", wich is a [jocular] word meaning "transform".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.