'int'对象不可迭代:当我尝试使用Python在Cassandra中插入数据时 [英] 'int' object is not iterable : When I try to insert data in Cassandra using Python

查看:67
本文介绍了'int'对象不可迭代:当我尝试使用Python在Cassandra中插入数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python将数据插入Cassandra表时,我遇到了一些问题.

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

代码:

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

推荐答案

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

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

Cassandra的

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

这篇关于'int'对象不可迭代:当我尝试使用Python在Cassandra中插入数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆