python - 如何在 Python 中将 dict 绑定(bind)到准备好的 Cassandra 语句?

标签 python cassandra cassandra-driver

如何传递字典和查询参数?

我可以编写这样的代码。

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/

相关文章:

Python打印SQL语句结果

python - 使用 Hadoop 流式处理线程

python - 如何将 JSON Web token (JWT) 传递给获取请求

python - 使用 REST 端点将数据迁移到 Cassandra

node.js - timeuuid 的 Cassandra 数据库导入问题

python - 如何为蜘蛛设置管道

cassandra - 如何配置 Cassandra 监听的接口(interface)?

c# - C# DataStax 驱动程序中的 CassandraEntityBase

java - Cassandra - 写入错误。怎么解决呢?

python - 哪个 Cassandra 驱动程序最适合 aiohttp?