python - cur 执行重复参数

标签 python sql mysql-python

我正在 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))

关于python - cur 执行重复参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42120674/

相关文章:

python - 将库从 Python 2.7 转换为 3.4

SQL Server : Filter for only the rows directly after a row containing specific text

python - 我不能得到?而不是 %s 来处理 python mysqldb 的插入

Python:无法将列表值插入 mysql 列

python - Python/numpy 中的 n 维网格

python - 返回两个字符串的字符差异

python - 需要帮助返回在函数外部使用的变量 (Python)

mysql按名称分组查询,按id排序,按id限制

sql - RAISERROR 引发替换参数错误

python-2.7 - Python MySQLdb-在查询中使用多个数据库表