Skip to content
Advertisement

How to refactor my python to use a SQL Prepared statement?

Code to accept input from the user and gives a User for a given username. Now it is accepting the input directly into the SQL query but I would like to use it with prepared Statements, how could I do it?

Advertisement

Answer

Consider adjusting your query methods to accept a parameter input and run in cursor.execute. This requires you replace string formatting of data value in SQL string

with a prepared statement of no data that is later binded with data via parameter(s).

Do not confuse unquoted %s with quoted '%s' placeholder (which the latter method as commented above is no longer recommended in Python). Altogether:

If no parameters is needed for calls pass None type.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement