1

我正在使用 nobonobo 的python 绑定到 unqlite,并且在尝试使用 JSON 文档集合时遇到了问题。

在自述文件中,有这个 JX9 脚本:

sample = (
    "db_create('users'); /* Create the collection users */"
    "db_store('users',{ 'name' : 'dean' , 'age' : 32 });"
    "db_store('users',{ 'name' : 'chems' , 'age' : 27 });"
    "print db_fetch_all('users')..'\n';"
    "while( ($rec = db_fetch('users')) != NULL ){"
    "  print $rec; print '\n';"
    "}"
)

这会正确打印每条记录:

[{"name":"dean","age":32,"__id":0},{"name":"chems","age":27,"__id":1}]
{"name":"dean","age":32,"__id":0}
{"name":"chems","age":27,"__id":1}

但是,当我尝试使用回调在 Python 中读取集合时,我得到了垃圾:

@unqlitepy.OutputCallback
def f(output, outlen, udata):
    output = (c_char*outlen).from_address(output).raw
    print locals()
    return unqlitepy.UNQLITE_OK
db.fetch_cb('users', f)

这是输出:

{'udata': None, 'output': 'a\x1e\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x02D\xa7\x83\x0b', 'outlen': 22L}

同样,如果我抓住光标并打印用户集合中的第一个用户,我会得到:

'users_0' '\x01\x08\x00\x00\x00\x04name\x05\x08\x00\x00\x00\x04dean\x06\x08\x00\x00\x00\x03age\x05\n\x00\x00\x00\x00\x00\x00\x00 \x06\x08\x00\x00\x00\x04__id\x05\n\x00\x00\x00\x00\x00\x00\x00\x00\x06\x02'

有人知道会发生什么吗?有没有办法解码返回给python的数据?

4

1 回答 1

1

我写了一些新的绑定,使这一切变得更容易:https ://github.com/coleifer/unqlite-python

>>> users.store([
...     {'name': 'Charlie', 'color': 'green'},
...     {'name': 'Huey', 'color': 'white'},
...     {'name': 'Mickey', 'color': 'black'}])
True
>>> users.store({'name': 'Leslie', 'color': 'also green'})
True

>>> users.fetch(0)  # Fetch the first record.
{'__id': 0, 'color': 'green', 'name': 'Charlie'}

>>> users.delete(0)  # Delete the first record.
True
>>> users.delete(users.last_record_id())  # Delete the last record.
True
>>> users.all()
[{'__id': 1, 'color': 'white', 'name': 'Huey'},
 {'__id': 2, 'color': 'black', 'name': 'Mickey'}]

>>> users.filter(lambda obj: obj['name'].startswith('H'))
[{'__id': 1, 'color': 'white', 'name': 'Huey'}]
于 2014-06-08T11:20:03.783 回答