python整形不可迭代,TypeError:'int'对象不可迭代-Python

I got the following error:

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

解决方案

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,)

The full line then'd be:

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])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值