如何传递字典和查询参数?
我可以编写这样的代码。
prepared = session.prepare('select name from task where id = ?;')
bound = prepared.bind([1])
session.execute(bound)
如何使用 dict 作为参数以及查询语法是什么?
这不起作用:
prepared = session.prepare('select name from task where id = %(id)s;')
bound = prepared.bind({"id": 1})
session.execute(bound)
你能帮忙处理这个基本代码吗 - 看起来这是可能的,但我不知道有效的查询语法?
最佳答案
query = """
INSERT INTO table_name (
field_1,
field_2
) VALUES (?, ?)
"""
cql_session.prepare(query).bind({'field_1': 'foo', 'field_2': 'bar'})
这对我们有用。
Docs说的是
bind(values)
Binds a sequence of values for the prepared statement parameters and returns this instance. Note that values must be:
- a sequence, even if you are only binding one value, or
- a dict that relates 1-to-1 between dict keys and columns
关于python - 如何在 Python 中将 dict 绑定(bind)到准备好的 Cassandra 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45836895/