Cassandra:'unicode'没有缓冲接口

时间:2015-07-08 07:54:36

标签: python unicode cassandra prepared-statement

我尝试在我的django app中使用这两个准备好的语句:

READINGS = "SELECT * FROM readings"
READINGS_BY_USER_ID = "SELECT * FROM readings WHERE user_id=?"

我使用以下命令查询数据库:

def get_all(self):
    query = self.session.prepare(ps.ALL_READINGS)
    all_readings = self.session.execute(query)

    return all_readings

def get_all_by_user_id(self, user_id):
    query = self.session.prepare(ps.READINGS_BY_USER_ID)
    readings = self.session.execute(query, [user_id])

    return readings

两者中的第一个效果相当不错。但第二个给了我:

ERROR 2015-07-08 09:42:56,634 | views::exception_handler 47 | ('Unable to complete the operation against any hosts', {<Host: localhost data1>: TypeError("'unicode' does not have the buffer interface",)})

谁能告诉我这里发生了什么?我明白,某个地方必须有一个没有缓冲接口的unicode字符串。但是哪个字符串是指?我准备好的陈述?

此外还有堆栈跟踪:

   Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/views.py", line 448, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/me/Workspace/project/Readings/views.py", line 36, in get_by_user_id
    readings = self.tr_dao.get_all_by_user_id(user_id)
  File "/Users/me/Workspace/project/Readings/dao.py", line 22, in get_all_by_user_id
    readings = self.session.execute(query, [user_id], timeout=60)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cassandra/cluster.py", line 1405, in execute
    result = future.result(timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cassandra/cluster.py", line 2967, in result
    raise self._final_exception

2 个答案:

答案 0 :(得分:0)

如果你在python 2上,这可能会解决它

def get_all_by_user_id(self, user_id):
    query = self.session.prepare(ps.READINGS_BY_USER_ID)
    readings = self.session.execute(query, [str(user_id)])
    return readings

答案 1 :(得分:0)

这不起作用,因为您的user_id是unicode类型。您可以使用

进行检查

type(user_id)

如果是这种情况,您应该将其编码为字符串: str(user_id)

它将解决问题。