【发布时间】:2015-04-06 14:01:06
【问题描述】:
我收到以下错误:
File "/home/ec2-user/test/test_stats.py", line 43, in get_test_ids_for_id
cursor.execute("""select test_id from test_logs where id = %s """, (id))
File "/home/ec2-user/.etl/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: 'int' object is not iterable
这是我遇到问题的代码部分:
def get_test_ids_for_id(prod_mysql_conn, id):
cursor = prod_mysql_conn.cursor()
cursor.execute("""select test_id from test_logs where id = %s """, (id))
rows = cursor.fetchall()
test_ids = []
for row in rows:
test_ids.append(row[0])
return test_ids
【问题讨论】:
-
应该是
cursor.execute(..., (id,))- 注意尾随的逗号,这使它成为一个元组。 -
@jonrsharpe 你能解释清楚吗?
-
Martijn 已经有了!参见例如stackoverflow.com/q/22460082/3001761
-
你好试试这样[id]
标签: python mysql python-2.7 mysql-python