在Python::Cassandra session.execute()语句的输入错误处没有可行的替代方法

2024-04-25 17:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在Cassandra的一个表中插入几条记录,并希望使用“.format()函数传递列名。但我面临以下错误。有人能告诉我这个问题吗

session.execute(
    """
    insert into test.student({})
    values(%s)
    """.format(mylist[0])
    [59])

错误:

File "cassandra/cluster.py", line 2240, in cassandra.cluster.Session.execute
File "cassandra/cluster.py", line 4198, in cassandra.cluster.ResponseFuture.result
cassandra.protocol.SyntaxException: <Error from server: code=2000 
[Syntax error in CQL query] message="line 0:-1 no viable alternative at input '<EOF>'">

Tags: 函数inpyformatexecutesession错误记录
1条回答
网友
1楼 · 发布于 2024-04-25 17:20:33

在insert语句中,驱动程序无法识别要替换的值,请尝试:

session.execute(
"""
insert into test.student({0})
values({1})
""".format(mylist[0], 59)

将被更新的列将是变量mylist的第一项,值为59

https://github.com/datastax/python-driver/blob/master/tests/integration/standard/test_query.py有多个使用python驱动程序的示例

相关问题 更多 >