Python:如何使用UnQLite存储字典?

2024-06-16 11:50:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: 字符串fromimport数据库db字典dictunqlite
1条回答
网友
1楼 · 发布于 2024-06-16 11:50:37

我发现我可以使用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}]

相关问题 更多 >