Skip to content
Advertisement

How to use variables in SQL statement in Python?

I have the following Python code:

where var1 is an integer, var2 and var3 are strings.

How can I write the variable names without Python including them as part of the query text?

Advertisement

Answer

Note that the parameters are passed as a tuple.

The database API does proper escaping and quoting of variables. Be careful not to use the string formatting operator (%), because

  1. it does not do any escaping or quoting.
  2. it is prone to Uncontrolled string format attacks e.g. SQL injection.
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement