如何从TinyDB中删除记录或文档 [英] How to delete record or document from TinyDB

查看:106
本文介绍了如何从TinyDB中删除记录或文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从TinyDB中删除记录或文档

How to delete record or document from TinyDB

数据库示例:

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

如果要id_key=='xxx'

在TinyDB上,建议使用以下代码.如何完成删除记录/文档的操作?

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

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

推荐答案

要对数据使用示例代码,请输入:

To use the example code for your data, type:

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

请注意:TinyDB是键值数据库.使用上面的代码将删除键"xxx".如果您输入:

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

db.all()

您将看到键"xxx"已删除.但要意识到该行仍存在于数据库中,并且如果其中的"params"中包含任何值,则"params"中的值仍将存在.

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.

更好的替代方法可能是使用TinyDB的remove命令,例如:

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

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

这篇关于如何从TinyDB中删除记录或文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆