使用Python Cassandra模块的参数化查询 [英] Parameterized queries with the Python Cassandra Module
问题描述
我一直在试用Python的CQL外挂程式( http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/ ),它支持参数化查询。从他们的文档:
I've been experimenting with the CQL plugin for Python (http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/) which has support for parameterized queries. From their documentation:
import cql
connection = cql.connect(host, port, keyspace)
cursor = connection.cursor()
cursor.execute("CQL QUERY", dict(kw='Foo', kw2='Bar', etc...))
我的问题是它是否可以在循环中多次参数化和执行同一查询,以及什么样的方法来完成。抱歉,文档很少,所以我正在搜索一个答案...
My question is whether its possible to parameterize and execute the same query multiple times in a loop, and what the methods look like to accomplish that. Sorry but documentation is scant so I'm searching about for an answer...
推荐答案
a href =http://code.google.com/a/apache-extras.org/p/cassandra-dbapi2/source/browse/#git/test =nofollow>测试了解更多示例
Take a look at the code in tests for more examples
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"))
或更具体地说, / p>
Or more specifically to your question about running it in a loop:
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)
这篇关于使用Python Cassandra模块的参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!