'int' object is not iterable : When I try to insert data in Cassandra using Python

大憨熊 提交于 2021-02-16 14:42:28

问题


I am facing some issue while inserting data to a Cassandra Table using Python.

Code:

session.execute(
    """
    insert into test.student(Student_id)
    values(%s)
    """,
    56)

Error:

  File "cassandra/cluster.py", line 2239, in cassandra.cluster.Session.execute
  File "cassandra/cluster.py", line 2279, in cassandra.cluster.Session.execute_async
  File "cassandra/cluster.py", line 2343, in cassandra.cluster.Session._create_response_future
  File "cassandra/query.py", line 897, in genexpr
  File "cassandra/query.py", line 897, in genexpr
TypeError: 'int' object is not iterable

回答1:


As with any other ORM in python, the second argument of .execute must be a tuple or a list when using positional arguments:

session.execute(
    """
    insert into test.student(Student_id)
    values(%s)
    """,
    (56,))
#     ^ note the parenthesis and the REQUIRED trailing comma
#       to make this a single-element tuple

This is also mentioned in Cassandra's docs:

Note: you must always use a sequence for the second argument, even if you are only passing in a single variable

session.execute("INSERT INTO foo (bar) VALUES (%s)", "blah")  # wrong
session.execute("INSERT INTO foo (bar) VALUES (%s)", ("blah"))  # wrong
session.execute("INSERT INTO foo (bar) VALUES (%s)", ("blah", ))  # right
session.execute("INSERT INTO foo (bar) VALUES (%s)", ["blah"])  # right


来源:https://stackoverflow.com/questions/57770110/int-object-is-not-iterable-when-i-try-to-insert-data-in-cassandra-using-pyth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!