TindyDB

 

Welcome to TinyDB! — TinyDB 4.5.1 documentation

tinydb:Welcome to TinyDB! — TinyDB 4.5.1 documentation

 

 

from tinydb import TinyDB, Query
db = TinyDB(r'./db.json')
student = db.table("student")
student.insert({"name": "Tom", "age": 23})
student.insert_multiple([
    {"name": "Jack", "age": 24},
    {"name": "mary", "age": 25}
])


print(student.all())

query = Query()
result = student.search(query.name == 'Tom')
print(result)


result = student.search(query.age > 24)
print(result)
# [{'name': 'mary', 'age': 25}]

# 逻辑查询
User = Query()
db.search(~ (User.name == 'John'))  # Negate
db.search((User.name == 'John') & (User.age <= 30))  # And
db.search((User.name == 'John') | (User.name == 'Bob'))  # Or

# 更新
print("update........")
student.update({'age': 26}, query.name == "Tom")
print(student.search(query.name=="Tom"))
# [{'name': 'Tom', 'age': 26}]

# 删除
print("delete........")
student.remove(query.age < 25)
print(student.all())
# [{'name': 'Tom', 'age': 26}, {'name': 'mary', 'age': 25}]

# 关闭
db.close()

 

posted @ 2021-09-21 21:00  linbo.yang  阅读(83)  评论(0编辑  收藏  举报