Cha*_*eon 2 python cassandra cassandra-driver
如何传递字典和查询参数?
我可以做这样的代码。
prepared = session.prepare('select name from task where id = ?;')
bound = prepared.bind([1])
session.execute(bound)
Run Code Online (Sandbox Code Playgroud)
如何使用 dict 作为参数以及查询语法是什么?
这不起作用:
prepared = session.prepare('select name from task where id = %(id)s;')
bound = prepared.bind({"id": 1})
session.execute(bound)
Run Code Online (Sandbox Code Playgroud)
你能帮忙解决这个基本代码吗 - 看起来这是可能的,但我不知道有效的查询语法?
query = """
INSERT INTO table_name (
field_1,
field_2
) VALUES (?, ?)
"""
cql_session.prepare(query).bind({'field_1': 'foo', 'field_2': 'bar'})
Run Code Online (Sandbox Code Playgroud)
这对我们有用。
文件说
绑定(值)
绑定准备好的语句参数的值序列并返回此实例。请注意,值必须是:
- 一个序列,即使您只绑定一个值,或者
- 字典键和列之间一对一关联的字典