【问题标题】:Python: How to store a dictionary with UnQLite?Python:如何使用 UnQLite 存储字典?
【发布时间】:2017-09-28 07:49:29
【问题描述】:

我想将字典存储在 UnQLite 数据库中,以便以后使用。但是 UnQLite 将其存储为字符串:

>>> from unqlite import UnQLite
>>> db = UnQLite()
>>> db['dict'] = {'a': 5}
>>> db['dict']
"{'a': 5}"

【问题讨论】:

    标签: python dictionary unqlite database nosql


    【解决方案1】:

    我发现我可以使用collection,然后使用其本机类型检索数据。

    >>> from unqlite import UnQLite
    >>> db = UnQLite()
    >>> colors = db.collection('colors')
    >>> if not colors.exists():
    ...   colors.create()
    ... 
    >>> colors.store([{'name': 'red', 'code': '#ff0000'}, {'name': 'green', 'code': '#00ff00'}])
    1
    >>> colors.all()
    [{'code': '#ff0000', 'name': 'red', '__id': 0}, {'code': '#00ff00', 'name': 'green', '__id': 1}]
    

    【讨论】:

    • 您也不需要在 create() 之前检查是否存在 - “如果集合不存在,请创建它。”
    猜你喜欢
    • 1970-01-01
    • 2020-07-28
    • 2011-10-29
    • 2016-02-21
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多