我在python中使用CodernityDB。在
我想创建一个表,其中表中的一个索引将包含字符串而不是整数。在
我该怎么做?在
以下是我正在使用的教程:
#!/usr/bin/env python
from CodernityDB.database import Database
from CodernityDB.hash_index import HashIndex
class WithXIndex(HashIndex):
def __init__(self, *args, **kwargs):
kwargs['key_format'] = 'I'
super(WithXIndex, self).__init__(*args, **kwargs)
def make_key_value(self, data):
a_val = data.get("x")
if a_val is not None:
return a_val, None
return None
def make_key(self, key):
return key
def main():
db = Database('/tmp/tut2')
db.create()
x_ind = WithXIndex(db.path, 'x')
db.add_index(x_ind)
for x in xrange(100):
db.insert(dict(x=x))
for y in xrange(100):
db.insert(dict(y=y))
print db.get('x', 10, with_doc=True)
if __name__ == '__main__':
main()
基本上,我想把字符串值放在x中,而不是整数值,当我尝试这样做时,我收到一条错误消息:“Cannot convert argument to integer”。在
我确信这是一件很容易的事,但我在他们的文件中找不到。在
可能我只需要在make_key_value函数中添加一个str转换,但不确定它是否能工作。在
https://bitbucket.org/codernity/codernitydb-demos/src/1bf5ae166fa5/minitwit/?at=default
有一些例子,看看迷你网.py调用数据库_索引.py在
数据库_索引.py: 有一些在键值对中存储数据的方法
在迷你网.py调用它们并插入到数据库中,查看注册函数。在
我自己也在想办法
这是mini-twit函数的浓缩版,我用它来做实验。在
createDB,顾名思义,创建一个数据库并允许您用数据填充它。在
requestDat,有一些对数据库的查询示例。 [如果您提出了更多的查询来缩小实际数据值,比如从id索引plz post]
在创建数据库.py在
在请求日期.py在
^{pr2}$相关问题 更多 >
编程相关推荐