当我尝试将元组传递给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
从元组切换到列表不会影响结果。我该如何传递这些论点?
答案 0 :(得分:0)
如何将字符串创建为字符串然后执行此操作:
accounts = "('Hyvaco','TLC')"
sql ='SELECT * FROM archive.incoming WHERE LastCapacity=%s AND Account IN '+ accounts