Python驱动程序Cassandra查询错误

时间:2015-01-28 05:15:15

标签: python python-2.7 cassandra

我在通过python驱动程序查询我的cassandra数据库时遇到问题。

我的cassandra版本是 python2.7

>>> import cassandra
>>> cassandra.__version__
'2.1.3'
>>>

我的代码:

def search_data(self, ticker):
      row = self.session.execute("""select * from secmaster.ticker_name where ticker = %s""",(ticker))
      print row

获取错误:

  File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1293, in execute
    future = self.execute_async(query, parameters, trace)
  File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1342, in execute_async
    future = self._create_response_future(query, parameters, trace)
  File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1373, in _create_response_future
    query_string = bind_params(query_string, parameters, self.encoder)
  File "/usr/local/lib/python2.7/dist-packages/cassandra/query.py", line 727, in bind_params
    return query % tuple(encoder.cql_encode_all_types(v) for v in params)
TypeError: not all arguments converted during string formatting

但如果我在下面查询它确实有效

 def search_data(self, ticker):
          row = self.session.execute("""select * from secmaster.ticker_name""")
          print row

2 个答案:

答案 0 :(得分:3)

必须使用序列(例如Session.execute)或parameters调用

tuple参数dict。请注意,将单个表达式括在括号中并不会使它成为单项元组;你必须添加一个尾随逗号,例如:

def search_data(self, ticker):
      row = self.session.execute("""select * from secmaster.ticker_name where ticker = %s""",(ticker,))
      print row

答案 1 :(得分:-3)

固定

row = self.session.execute(" select * from secmaster.ticker_name,其中ticker ='%s'"%ticker)