【TinyDB】入门——让你开心的面向文档的微型数据库

入门

     :Welcome to TinyDB, your tiny, document oriented database optimized for your happiness :)

【官方文档】:https://tinydb.readthedocs.io/en/latest/index.html

  4.0.0 版本后有一些变化,需要仔细看官方文档。

 

      在之前的项目中,使用了TinyDB库来帮助自己实现数据库支持,记得当时找了很多数据库,什么MySQL,MongoDB等等,结果还需要安装各种程序,配置各种环境,想想我一个程序处理的数据总共不超过1000个,搞这么复杂的数据库学习成本太高。最后找到了TinyDB,不吹牛逼,TinyDB真的很适合小型项目,对我这样的初学者还是比较友好的。

基本认识

【为什么要用】:如果您需要一个简单数据库,该数据库具有干净的 API,无需大量配置即可工作,则 TinyDB 可能是您的最佳选择。

【为什么不用】:如果你需要高级功能或高性能,TinyDB是一个错误的数据库。

安装

pip install tinydb

基础操作

导入库 TinyDB() 提供数据库处理功能,Query()提供查询查询功能,两者怎么建立联系还没搞懂。

from tinydb import TinyDB #
from tinydb import Query #

1、生成数据库对象

db = TinyDB( 'db.json')
'''
1、形成一个db.json 文件
2、json文件的内容:{"_default": {}}
'''

TinyDB构建的数据架构就是用字典不断往里面加字典,如果不新增table(),db实际上操作的是table(_default)

2、写数据

# 2.1 一次写一条数据
db.insert({'type': 'apple', 'count': 7})
el = db.insert({"type": "peach", "count": 3})
print(el) # >>>:2          返回key
'''
    1、传入的数据形式应该是字典:{数据}
    2、{数据}作为value被传入,对应的key是 '1'、'2'、'3'...,依次排下去
    json文件的内容:
        {"_default": {
                "1": {"type": "apple", "count": 7},
                "2": {"type": "peach", "count": 3}}
        }
'''
# 2.2 一次写多条数据
# 用列表一次传多条数据,列表的元素是字典: [{},{},{}]
em = db.insert_multiple(
    [
        {'name': 'John', 'age': 22},
        {'name': 'John', 'age': 22},
        {"type": "peach", "count": 3}
    ]
)
print(em) # >>>:[3, 4, 5]    一次写多条,返回的是列表

3、读数据

# 3.1 一次读取所有数据
db.all()
print(db.all())
# 返回值是一个列表
'''
[
    {'type': 'apple', 'count': 7},
    {'type': 'peach', 'count': 3},
    {'name': 'John', 'age': 22},
    {'name': 'John', 'age': 22},
    {'type': 'peach', 'count': 3}
]
'''
# 3.2 遍历所有数据
for item in db:
    print(item)
'''
{'type': 'apple', 'count': 7}
{'type': 'peach', 'count': 3}
{'name': 'John', 'age': 22}
{'name': 'John', 'age': 22}
{'type': 'peach', 'count': 3}
'''

 4、查数据

'''
    需要用的Query()
'''
# 4.1 查询 (==, !=, >, >=, <, <=)
Q = Query()
db.search(Q.type == 'apple')
db.insert({
'名字':'桃子'}) p = db.search(Q.名字 == '桃子') print(p) ''' 结果是包含了要查询的字典的列表:[{'type': 'apple', 'count': 7}] 注意: key用的是中文,也可以查询 '''

Query()是提供查询的功能,要跟TinyDB()一起用才行,感觉是建立了一个索引实例。

5、改数据

db.update({'名字':'苹果'}, Q.名字 =='桃子')
print(db.all())

 db.update(新字典,条件) ,这里按照条件返回的是整个符合条件的字典,一换就全换了,不是只改字典里的键值对。

6、删数据

# 6.1 删一条,或者说删符合条件的
a = db.remove(Q.名字 == '苹果')
print(db.all())
# 6.2 清空所有数据
db.purge()
print(db.all())

7、记住这张表

Inserting
db.insert(...) Insert a document 插入一个文档
Getting data
db.all() Get all documents 读取所有文档
iter(db) Iter over all documents db可以迭代,进行遍历
db.search(query) Get a list of documents matching the query 读取符合query条件的文档列表
Updating
db.update(fields, query) Update all documents matching the query to contain fields
Removing
db.remove(query) Remove all documents matching the query  删符合条件的所有文档
db.truncate() Remove all documents 清空所有文档
Querying
Query() Create a new query object 创建一个查询对象
Query().field == 2 Match any document that has a key field with value == 2 (also possible: != > >= < <=)

 

2020-03-01

下一节:【学库】TinyDB(查询)

 

 

posted @ 2020-03-02 20:24  王大桃zzZ  阅读(6312)  评论(2编辑  收藏  举报