How to delete record or document from TinyDB

China☆狼群 提交于 2019-12-05 00:05:38

问题


How to delete record or document from TinyDB

Example of DB:

{"1" : {"id_key" : "xxx", "params" : {} } },
{"2" : {"id_key" : "yyy", "params" : {} } },

I want to delete "1" if id_key=='xxx'

On TinyDB tutorial code below is suggested. How to complete it to delete record/document ?

db.update(delete('key1'), where('key') == 'value')

回答1:


To use the example code for your data, type:

db.update(delete('id_key'), where('id_key') == 'xxx')

Please note: TinyDB is a key-value database. using the code above will remove the key 'xxx'. If you type:

db.all()

you will see that the key 'xxx' is deleted. but realize that the row still exists in the database AND if 'params' had any values in it, the values in 'params' would still exist.

A better alternative might be to use TinyDB's remove command, for example:

db.remove(where('id_key') == 'xxx')


来源:https://stackoverflow.com/questions/31022581/how-to-delete-record-or-document-from-tinydb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!