1

当我尝试将元组传递给子句的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到列表不会影响结果。我应该如何传递这些论点?

4

1 回答 1

0

您如何将帐户创建为字符串,然后执行以下操作:

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

sql ='SELECT * FROM archive.incoming WHERE LastCapacity=%s AND Account IN '+ accounts
于 2013-11-14T18:15:49.463 回答