1

nobonobo のpython バインディングを unqliteに使用していますが、JSON ドキュメント コレクションを操作しようとすると問題が発生します。

README には、次の 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 に答える