python列出没有for循环的sql语句中列表的值

时间:2015-08-05 01:12:02

标签: python mysql loops teradata

这是我的数据:

mylist=["happy" , "sad", "cute"]
country = 'US'

mytable的

id terms values
1   happy  3
2   sad    4
3   angry  5

在python中我的sql查询如下所示:

    myquery= 
"""select * from mytable where country = '%s' and terms is in ('%s', '%s');""" %(country, tuple(mylist)) 

错误的格式字符串参数不够。

1 个答案:

答案 0 :(得分:0)

python的db adapter提供了参数格式化,你只需要使用它:

cursor.execute('select * from mytable where terms in %s and country = %s;', (tuple(mylist), country))

查看更多:https://www.python.org/dev/peps/pep-0249/#id14