Tinydb 简明教程
TinyDB - Storage Types
TinyDB 具有两种类型的存储:JSON 和内存中。TinyDB 默认情况下将数据存储在 JSON 文件中。在创建数据库时,您需要指定在计算机上存储 JSON 文件的路径。
TinyDB has two types of storage: JSON and in-memory. TinyDB, by default, stores the data in JSON files. While creating a database, you need to specify the path where to store the JSON file on your computer.
Storing Data in a JSON File
首先,让我们看看如何使用 JSON 文件存储数据 −
First, let’s see how we can use a JSON file to store the data −
from tinydb import TinyDB, where
db = TinyDB('path/to/file_name.json')
Example 1
在此示例中,我们展示了如何将多个文档插入到 JSON 文件中 −
In this example, we are showing how you can insert multiple documents into a JSON file −
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 个文档。要验证记录,请使用以下查询 −
Here, we inserted 5 documents inside "storage.json". To verify the records, use the following query −
db.all()
它将显示“storage.json”文件的内容 −
It will show the contents of "storage.json" file −
[
{
"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"
}
]
Using in-memory to Store Data
现在,让我们看看如何使用“ in-memory ”存储数据 −
Now, let’s see how we can use "in-memory" to store the data −
from tinydb import TinyDB
from tinydb.storages import MemoryStorage
object = TinyDB(storage = MemoryStorage)
TinyDB.DEFAULT_STORAGE = MemoryStorage
Example 2
以下示例展示了如何将多个文档插入到默认存储内存中 −
The following example shows how you can insert multiple documents in default storage memory −
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"
}
])
要验证是否已插入文档,请使用以下查询 −
To verify whether the documents have been inserted or not, use the following query −
object.all()
以下 output 显示了已插入数据 −
The following output shows the inserted data −
[
{
"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"
}
]