带有Python元组的Mysql SELECT IN子句
Python新手…我想从for循环创建一个新的元组。元组将在带有Python元组的Mysql SELECT IN子句,python,Python,Python新手…我想从for循环创建一个新的元组。元组将在in子句中的MySQL select查询中使用。它工作不正常,告诉我在cur.execute行上有以下问题 TypeError:在字符串格式化过程中并非所有参数都已转换 check_tz = [] for tz in pytz.common_timezones: if now_utc.astimezone(timezone(tz)).strftime('%H') == '01': check_tz.append(
in
子句中的MySQL select查询中使用。它工作不正常,告诉我在cur.execute
行上有以下问题
TypeError:在字符串格式化过程中并非所有参数都已转换
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]
您可能需要将时区列表转换为字符串:
cur.execute("SELECT * FROM users where timezone IN (%s)", ','.join(map(lambda x: "'%s'" % x, check_tz))
不起作用,链接到可能的重复项中的解决方案完全起作用。@幸运的是,它确实缺少了元素的引用。是的。很好用!