“int”对象不是iterable:当我尝试使用python在cassandra中插入数据时

z9zf31ra  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(271)

我在使用python向cassandra表插入数据时遇到了一些问题。
代码:

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

错误:

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
bbmckpt7

bbmckpt71#

与python中的任何其他orm一样 .execute 使用位置参数时必须是元组或列表:

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

Cassandra的文件中也提到了这一点:
注意:第二个参数必须始终使用序列,即使只传入一个变量

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

相关问题