Python MySQLdb占位符语法 [英] Python MySQLdb placeholders syntax
本文介绍了Python MySQLdb占位符语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用占位符,如以下示例所示:
I'd like to use placeholders as seen in this example:
cursor.execute ("""
UPDATE animal SET name = %s
WHERE name = %s
""", ("snake", "turtle"))
除了我想将查询作为自己的变量外,因为我需要将查询插入多个数据库中,如:
Except I'd like to have the query be its own variable as I need to insert a query into multiple databases, as in:
query = """UPDATE animal SET name = %s
WHERE name = %s
""", ("snake", "turtle"))
cursor.execute(query)
cursor2.execute(query)
cursor3.execute(query)
做这样的事情的正确语法是什么?
What would be the proper syntax for doing something like this?
推荐答案
query = """UPDATE animal SET name = %s
WHERE name = %s
"""
values = ("snake", "turtle")
cursor.execute(query, values)
cursor2.execute(query, values)
或者如果您想将它们分组在一起...
or if you want group them together...
arglist = [query, values]
cursor.execute(*arglist)
cursor2.execute(*arglist)
但是第一种方法可能更易读.
but it's probably more readable to do it the first way.
这篇关于Python MySQLdb占位符语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文