TypeError:'int'对象不可迭代-Python [英] TypeError: 'int' object is not iterable - Python
本文介绍了TypeError:'int'对象不可迭代-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到以下错误:
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
这是我遇到问题的代码部分:
Here's the section of my code I'm having trouble with:
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
一个元组,但只给它一个整数:
You need to give cursor.execute
a tuple, but you only gave it one integer:
(id)
添加一个逗号使其成为一个元组:
Add a comma to make that a tuple:
(id,)
完整的行将是:
cursor.execute("""select test_id from test_logs where id = %s """, (id,))
将表达式放在括号中只是将一个表达式分组".是逗号将某些内容变成元组:
Putting an expressione in parentheses just 'groups' that one expression. It is the comma that makes something a tuple:
>>> (42)
42
>>> (42,)
(42,)
任何可迭代的方法都可以做到,因此您也可以使用[...]
括号:
Any iterable will do really, so you could also use [...]
brackets:
cursor.execute("""select test_id from test_logs where id = %s """, [id])
这篇关于TypeError:'int'对象不可迭代-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文