我是靠谱客的博主 安详小白菜,最近开发中收集的这篇文章主要介绍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])

最后

以上就是安详小白菜为你收集整理的python整形不可迭代,TypeError:'int'对象不可迭代-Python的全部内容,希望文章能够帮你解决python整形不可迭代,TypeError:'int'对象不可迭代-Python所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部