1

我一直在试验 Python 的 CQL 插件(http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/),它支持参数化查询。从他们的文档中:

import cql
connection = cql.connect(host, port, keyspace)
cursor = connection.cursor()
cursor.execute("CQL QUERY", dict(kw='Foo', kw2='Bar', etc...))

我的问题是是否可以在一个循环中多次参数化和执行相同的查询,以及实现这一点的方法是什么样的。对不起,但文档很少,所以我正在寻找答案......

4

1 回答 1

2

查看测试中的代码以获取更多示例

import cql
connection = cql.connect(host, port, keyspace)
cursor = connection.cursor()

query = "UPDATE StandardString1 SET :c1 = :v1, :c2 = :v2 WHERE KEY = :key"
cursor.execute(query, dict(c1="ca1", v1="va1", c2="col", v2="val", key="ka"))
cursor.execute(query, dict(c1="cb1", v1="vb1", c2="col", v2="val", key="kb"))
cursor.execute(query, dict(c1="cc1", v1="vc1", c2="col", v2="val", key="kc"))
cursor.execute(query, dict(c1="cd1", v1="vd1", c2="col", v2="val", key="kd"))

或者更具体地说,您关于在循环中运行它的问题:

import cql
connection = cql.connect(host, port, keyspace)
cursor = connection.cursor()

query = "UPDATE StandardString1 SET :c1 = :v1, :c2 = :v2 WHERE KEY = :key"
values = [dict(c1="ca1", v1="va1", c2="col", v2="val", key="ka"),
          dict(c1="cb1", v1="vb1", c2="col", v2="val", key="kb"),
          dict(c1="cc1", v1="vc1", c2="col", v2="val", key="kc"),
          dict(c1="cd1", v1="vd1", c2="col", v2="val", key="kd")]

for value in values:
    cursor.execute(query, value)
于 2013-03-28T19:13:30.633 回答