- 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 - Retrieve Data
There are numerous ways with the help of which you can retrieve data from a TinyDB database. But to use those ways, you first need to create an instance of the Query class as follows −
from tinydb import Query Student = Query()
Here, Student is the name of the database.
Let s check the various ways to retrieve the information from a TinyDB database.
Data Retrieval Using the Search() Method
The search() method, as its name imppes, returns the pst of items that mathches the query we provided, otherwise it will return an empty pst.
For this and other examples, we will be using the following student database 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": "TinyDB", "address": "benglore" } ]
Let s take an example to understand the search() method −
from tinydb import TinyDB, Query db = TinyDB("leekha.json") student = Query() db.search(student.subject == TinyDB )
The above query will retrieve the the following output from the student database −
[ { "roll_number":1, "st_name":"elen", "mark":250, "subject":"TinyDB", "address":"delhi" }, { "roll_number":5, "st_name":"karan", "mark":275, "subject":"TinyDB", "address":"benglore" } ]
Data Retrieval Using the get() Method
As opposed to the search() method, the get() method returns only one matching document. It will return None, otherwise. For example, let s take the following code −
from tinydb import TinyDB, Query student = Query() db.get(student.subject == TinyDB )
The above query will retrieve the following data from the student database.
[{ roll_number : 1, st_name : elen , mark : 250, subject : TinyDB , address : delhi }]
Data Retrieval using the all() Method
The all() method returns all the documents in the database. For example,
db.all()
It will retrieve the entire data from the student database.
[ { "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":"TinyDB", "address":"benglore" } ]
Data Retrieval Using the for Loop
The for loop also returns all the documents in the database. For example,
for info in db: print(info)
Just pke the all() method, it will retrieve all the rows from the student database.
Advertisements