MySQL python规范化的元组在查询中失败

时间:2013-11-14 17:58:51

标签: python mysql-python

当我尝试将元组传递给IN子句的WHERE参数时,它会被双引号,因此我的查询失败。例如,如果我这样做,

# Connect to DB
import MySQLdb
cnxn = MySQLdb.connect(connectString)
curs = cnxn.cursor()
# Setup query
accounts = ('Hyvaco','TLC')
truck_type = 'fullsize'
query_args = (truck_type, accounts)
sql ='SELECT * FROM archive.incoming WHERE LastCapacity=%s AND Account IN %s'
# Run query and print
curs.execute(sql, query_args)
print(curs._executed)

然后我返回零行,查询打印为

SELECT * FROM archive.incoming WHERE LastCapacity='fullsize'
   AND Account IN ("'Hyvaco'", "'TLC'")

accounts从元组切换到列表不会影响结果。我该如何传递这些论点?

1 个答案:

答案 0 :(得分:0)

如何将字符串创建为字符串然后执行此操作:

accounts = "('Hyvaco','TLC')"

sql ='SELECT * FROM archive.incoming WHERE LastCapacity=%s AND Account IN '+ accounts