问题
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