cur 执行重复参数

cur execute repeating parameter

我正在 python 中执行 SQL 查询。表示重复参数的正确方法是什么? 示例:我有

cur.execute("""select * from TableA where field1= '{}' and field2 = '{}'""".format(a,a))

这里'a'和'a'是一样的。我需要在参数列表中重复它还是有什么方法只给它一次。

将数字放在括号内以引用位置参数。

safe_a = MySQLdb.escape_string(a) # protect against sql-injection!!!
cur.execute("select * from TableA where field1 = '{0}' and field2 = '{0}'".format(safe_a))