English 中文(简体)
TinyDB - Storage Types
  • 时间:2024-11-03

TinyDB - Storage Types


Previous Page Next Page  

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