2013-07-12 64 views
0

Python新手在这里...我想从for循环创建一个新的元组。该元组将在IN子句中的MySQL select查询中使用。这工作不正确,告诉我我在cur.execute行有以下问题。Mysql与Python元组的SELECT IN子句

TypeError: not all arguments converted during string formatting

check_tz = [] 
for tz in pytz.common_timezones: 
    if now_utc.astimezone(timezone(tz)).strftime('%H') == '01': 
     check_tz.append(tz) 

print check_tz 

# Prints out something as follows. I cut the output short for the sake of this example. 
# ['America/Anguilla', 'America/Antigua', 'US/Eastern'] 

cur.execute("SELECT * FROM users where timezone IN (%s)", check_tz) 

for row in cur.fetchall(): 
    print row[0] 
+0

没错。完美的作品! – luckytaxi

回答

1

你可能需要的时区的列表转换为字符串:

cur.execute("SELECT * FROM users where timezone IN (%s)", ','.join(map(lambda x: "'%s'" % x, check_tz)) 
+0

不起作用,链接到可能的重复的解决方案完美地工作。 – luckytaxi

+0

@luckytaxi事实上,它缺少元素的引用。 – plaes