- TinyDB - Discussion
- TinyDB - Useful Resources
- TinyDB - Quick Guide
- TinyDB - Extensions
- TinyDB - Extend TinyDB
- TinyDB - Middleware
- TinyDB - Storage Types
- TinyDB - Caching Query
- TinyDB - Default Table
- TinyDB - Tables
- TinyDB - Document ID
- TinyDB - Retrieving Data
- TinyDB - Upserting Data
- TinyDB - Modifying the Data
- TinyDB - Handling Data Query
- TinyDB - Logical OR
- TinyDB - Logical AND
- TinyDB - Logical Negate
- TinyDB - The one_of() Query
- TinyDB - The All() Query
- TinyDB - The Any() Query
- TinyDB - The Test() Query
- TinyDB - The Matches() Query
- TinyDB - The Exists() Query
- TinyDB - The where Clause
- TinyDB - Searching
- TinyDB - Querying
- TinyDB - Delete Data
- TinyDB - Update Data
- TinyDB - Retrieve Data
- TinyDB - Insert Data
- TinyDB - Environmental Setup
- TinyDB - Introduction
- TinyDB - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TinyDB - Storage Types
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
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
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" } ])
Here, we inserted 5 documents inside "storage.json". To verify the records, use the following query −
db.all()
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
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()
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" } ]Advertisements