【问题标题】:Mysql SELECT IN clause with Python tuple [duplicate]带有Python元组的Mysql SELECT IN子句[重复]
【发布时间】:2013-07-12 05:35:40
【问题描述】:

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

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]

【问题讨论】:

  • 是的。完美运行!

标签: python


【解决方案1】:

您可能需要将时区列表转换为字符串:

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

【讨论】:

  • 不起作用,可能重复的链接中的解决方案完美。
  • @luckytaxi 确实,它缺少元素的引用。
猜你喜欢
  • 2011-11-24
  • 1970-01-01
  • 2010-12-07
  • 2016-06-08
  • 2018-03-08
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
相关资源
最近更新 更多