- TinyDB 教程
- TinyDB - 主页
- TinyDB - 简介
- TinyDB - 环境设置
- TinyDB - 插入数据
- TinyDB - 检索数据
- TinyDB - 更新数据
- TinyDB - 删除数据
- TinyDB - 查询
- TinyDB - 搜索
- TinyDB - where 子句
- TinyDB - Exists() 查询
- TinyDB - Matches() 查询
- TinyDB - Test() 查询
- TinyDB - Any() 查询
- TinyDB - All() 查询
- TinyDB - one_of() 查询
- TinyDB - 逻辑否定
- TinyDB - 逻辑 AND
- TinyDB - 逻辑 OR
- TinyDB - 处理数据查询
- TinyDB - 修改数据
- TinyDB - 更新插入数据
- TinyDB - 检索数据
- TinyDB - 文档 ID
- TinyDB - 表
- TinyDB - 默认表
- TinyDB - 缓存查询
- TinyDB - 存储类型
- TinyDB - 中间件
- TinyDB - 扩展 TinyDB
- TinyDB - 扩展
- TinyDB 有用资源
- TinyDB - 快速指南
- TinyDB - 有用资源
- TinyDB - 讨论
TinyDB - 存储类型
TinyDB 有两种存储类型:JSON 和内存中。默认情况下,TinyDB 将数据存储在 JSON 文件中。创建数据库时,你需要指定在计算机上存储 JSON 文件的路径。
在 JSON 文件中存储数据
首先,我们看看如何使用 JSON 文件存储数据 -
from tinydb import TinyDB, where db = TinyDB('path/to/file_name.json')
示例 1
此示例展示了如何将多个文档插入 JSON 文件 -
from tinydb import TinyDB db = TinyDB('storage.json') db.insert_multiple([ { "roll_number":1, "st_name":"elen", "mark":250, "subject":"TinyDB", "address":"delhi" }, { "roll_number":2, "st_name":"Ram", "mark":[ 250, 280 ], "subject":[ "TinyDB", "MySQL" ], "address":"delhi" }, { "roll_number":3, "st_name":"kevin", "mark":[ 180, 200 ], "subject":[ "oracle", "sql" ], "address":"keral" }, { "roll_number":4, "st_name":"lakhan", "mark":200, "subject":"MySQL", "address":"mumbai" }, { "roll_number":5, "st_name":"karan", "mark":275, "subject":"oracle", "address":"benglore" } ])
在这里,我们在 "storage.json" 中插入了 5 个文档。要验证记录,请使用以下查询 -
db.all()
它将显示 "storage.json" 文件的内容 -
[ { "roll_number":1, "st_name":"elen", "mark":250, "subject":"TinyDB", "address":"delhi" }, { "roll_number":2, "st_name":"Ram", "mark":[ 250, 280 ], "subject":[ "TinyDB", "MySQL" ], "address":"delhi" }, { "roll_number":3, "st_name":"kevin", "mark":[ 180, 200 ], "subject":[ "oracle", "sql" ], "address":"keral" }, { "roll_number":4, "st_name":"lakhan", "mark":200, "subject":"MySQL", "address":"mumbai" }, { "roll_number":5, "st_name":"karan", "mark":275, "subject":"oracle", "address":"benglore" } ]
使用内存中存储数据
现在,我们看看如何使用 "内存中" 存储数据 -
from tinydb import TinyDB from tinydb.storages import MemoryStorage object = TinyDB(storage = MemoryStorage) TinyDB.DEFAULT_STORAGE = MemoryStorage
示例 2
以下示例展示了如何将多个文档插入默认存储内存中 -
from tinydb import TinyDB from tinydb.storages import MemoryStorage object = TinyDB(storage = MemoryStorage) TinyDB.DEFAULT_STORAGE = MemoryStorage object.insert_multiple([ { "roll_number":1, "st_name":"elen", "mark":250, "subject":"TinyDB", "address":"delhi" }, { "roll_number":2, "st_name":"Ram", "mark":[ 250, 280 ], "subject":[ "TinyDB", "MySQL" ], "address":"delhi" }, { "roll_number":3, "st_name":"kevin", "mark":[ 180, 200 ], "subject":[ "oracle", "sql" ], "address":"keral" }, { "roll_number":4, "st_name":"lakan", "mark":200, "subject":"MySQL", "address":"mumbai" }, { "roll_number":5, "st_name":"karan", "mark":275, "subject":"oracle", "address":"benglore" } ])
要验证是否已插入文档,请使用以下查询 -
object.all()
以下 输出显示了已插入的数据 -
[ { "roll_number":1, "st_name":"elen", "mark":250, "subject":"TinyDB", "address":"delhi" }, { "roll_number":2, "st_name":"Ram", "mark":[ 250, 280 ], "subject":[ "TinyDB", "MySQL" ], "address":"delhi" }, { "roll_number":3, "st_name":"kevin", "mark":[ 180, 200 ], "subject":[ "oracle", "sql" ], "address":"keral" }, { "roll_number":4, "st_name":"lakan", "mark":200, "subject":"MySQL", "address":"mumbai" }, { "roll_number":5, "st_name":"karan", "mark":275, "subject":"oracle", "address":"benglore" } ]
广告