我有一个列表,我想将列表的所有值传递到查询的 where 条件。我正在使用 Maya cmds 在 Maya Python 脚本编辑器中使用此
代码
list = [1,2,3]
db = MySQLdb.connect("host","root","password","test" )
for num in list:
cursor = db.cursor()
cursor.execute('select Name from project WHERE projectID = %s '%(num))
name = cursor.fetchone()
print(name)
cursor.close()
错误
Error: ProgrammingError: file C:\Program Files\Autodesk\Maya2014\Python\lib\site-packages\MySQLdb\connections.py line 38: 1064 #
最佳答案
尝试使用
cursor.executemany('select Name from project WHERE ProjectID = %s', list)
无需 for 循环。
这样做会提高性能,因为数据库不会在每个 for 循环中使用,而只会使用一次。
还要避免调用事物列表、元组等。
关于python - 如何在python mysql中循环获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48019927/