gpt4 book ai didi

python - cur 执行重复参数

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:05 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com