TinyDB – The Matches() Query

TinyDB – The matches() Query ”; Previous Next The matches() query matches the data from a JSON file with a given condition (in the form of a regular expression) and returns the results accordingly. It will return a blank value if the condition does not match with the data in the file. Syntax The syntax of TinyDB matches() is as follows − db.search(Query().field.matches(regular expression)) Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student. Let”s understand how it works with the help of a couple of examples. We will use the same student database that we have used in all the previous chapters. Example 1 Let”s see how we can use matches() for full item search. from tinydb import Query student = Query() db.search(student.st_name.matches(”[aZ]*”)) This query will fetch all the rows − [ { “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” } ] Example 2 Let”s see how we can use matches() for case-sensitive search. from tinydb import Query import re student = Query() db.search(student.st_name.matches(”ram”, flags=re.IGNORECASE)) This query will fetch the rows where the student name matches the string “ram”. Observe that we have used a flag to ignore the case while matching the strings. [{ ”roll_number”: 2, ”st_name”: ”Ram”, ”mark”: [250, 280], ”subject”: [”TinyDB”, ”MySQL”], ”address”: ”delhi” }] Example 3 Let”s see how we can use matches() for a particular item. student = Query() db.search(student.address.matches(”keral”)) This query will fetch the rows where the address matches the string “keral”. [{”roll_number”: 3, ”st_name”: ”kevin”, ”mark”: [180, 200], ”subject”: [”oracle”, ”sql”], ”address”: ”keral”}] Example 4 Let”s see what matches() would return when it does not find a particular item − student = Query() db.search(student.address.matches(”Ratlam”)) There are no rows where the “address” field matches the string “Ratlam”, hence it will return a blank value − [] Print Page Previous Next Advertisements ”;

TinyDB – The All() Query

TinyDB – The all() Query ”; Previous Next TinyDB provides a method called all() that finds an entire list of values as per the query provided. Let”s take an example and find out how it works. Syntax The syntax of TinyDB all() is as follows − db.search(Query().field.all(query|list) Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student. If we will provide a query as the argument of all() method, it will match all the documents where all documents in the list field match the given query. On the other hand, if we will provide a list as the argument of all() method, it will match all the documents where all documents in the list field are present in the given list. Let”s understand how it works with the help of a couple of examples. We will use the same student database that we have used in all the previous chapters. Example 1 Let”s see how we can find the fields from our student table where the subjects are both TinyDB, and MySQL − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.search(Query().subject.all([”TinyDB”, ”MySQL”])) This query will fetch the following row − [{ ”roll_number”: 2, ”st_name”: ”Ram”, ”mark”: [250, 280], ”subject”: [”TinyDB”, ”MySQL”], ”address”: ”delhi” }] Example 2 Let”s see how we can use all() to get the entire data from our database − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.all() It will fetch all the rows from the linked 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” } ] Print Page Previous Next Advertisements ”;

TinyDB – The Test() Query

TinyDB – The test() Query ”; Previous Next The test() query will test if the given arguments match with the data in a table. If it matches with the data, it will return the matched data, otherwise it will return blank. First of all, we need to define a test function and its arguments and then it will search the item in a given database. Syntax The syntax of TinyDB test() is as follows − db.search(Query().field.test(function or condition, *arguments)) Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student. We can create a custom test function as follows − object = lambda t: t == ”value” Here the lamba keyword is important to create the custom test function. Let”s understand how it works with the help of a couple of examples. We will use the same student database that we have used in all the previous chapters. Example 1 We will first create a test function and then use it in our student table − from tinydb import TinyDB, Query db = TinyDB(”student.json”) objects = lambda t: t == [250, 280] db.search(Query().mark.test(objects)) It will fetch the rows where the “mark” field has the values [250, 280] − [{”roll_number”: 2, ”st_name”: ”Ram”, ”mark”: [250, 280], ”subject”: [”TinyDB”, ”MySQL”], ”address”: ”delhi”}] Example 2 In this example, we will use the “subject” field in the test function − student = Query() db = TinyDB(”student.json”) objects = lambda t: t == ”TinyDB” db.search(student.subject.test(objects)) This query will fetch all the rows where the “subject” field has the value “TinyDB” − [ { “roll_number”:1, “st_name”:”elen”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”TinyDB”, “address”:”benglore” } ] Print Page Previous Next Advertisements ”;

TinyDB – Searching

TinyDB – Searching ”; Previous Next TinyDB provides the search() method to help you search any data from a document. Along with the query() object, the search() method can be used to find the data in a JSON file. We have various ways in which we can use the search() method on a TinyDB database. Method 1: TinyDB search() with Existence of a Field We can search the data from a database based on the existence of a field. Let”s understand it with an example. For this and other examples, we will be using the following 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” } ] Example The search query based on the existence of a field is as follows − from tinydb import Query student = Query() db.search(student.address.exists()) The above query will retrieve the following data from the student 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”:”lakan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”TinyDB”, “address”:”benglore” } ] Method 2: TinyDB search() with Regular Expression We can search for a particular data from a database using regular expression (Regex). Let”s understand how it works with a couple of examples. Example 1 Full item search matching the Regular Expression − from tinydb import Query student = Query() db.search(student.st_name.matches(”[aZ]*”)) This query will produce the following output − [ { “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” } ] Example-2 Case-sensitive search with Regular Expression − from tinydb import Query import re student = Query() db.search(student.st_name.matches(”lakan”, flags=re.IGNORECASE)) It wil produce the following output − [{ ”roll_number”: 4, ”st_name”: ”lakan”, ”mark”: 200, ”subject”: ”MySQL”, ”address”: ”mumbai” }] Example-3 Any part of the item matching with Regular Expression − from tinydb import Query student = Query() db.search(student.st_name.search(”r+”)) This query will produce the following output − [{ ”roll_number”: 5, ”st_name”: ”karan”, ”mark”: 275, ”subject”: ”TinyDB”, ”address”: ”benglore” }] Method 3: TinyDB search() using a Substring We can also use a substring while searching for a particular data from a TinyDB database. Let”s understand how it works with a couple of examples − Example-1 Take a look at this query; it will fetch the all the rows where the “address” field is “delhi”. from tinydb import Query student = Query() db.search(student[”address”] == ”delhi”) It will produce the following output − [ { “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” } ] Example-2 In this query, we have used a slightly different syntax for the search() method. from tinydb import Query student = Query() db.search(student.address.search(”mumbai”)) It will fetch all the rows where the “address” field is “mumbai”. [{ ”roll_number”: 4, ”st_name”: ”lakan”, ”mark”: 200, ”subject”: ”MySQL”, ”address”: ”mumbai” }] Print Page Previous Next Advertisements ”;

TinyDB – Update Data

TinyDB – Update Data ”; Previous Next TinyDB can store data in many formats and we can easily reterive the stored data using various methods. But sometimes, we need to update the data, for which we can use the update() method. For updating the database, we first need to create an instance of the Query class. You can use the following command for this purpose − from tinydb import Query Student = Query() Here, Student is the name of our database. The update() Method Here is the syntax for the update() method − db.update({ updated field: updated information… }, stable field: information) Let”s take an example to understand how the update() method works. For this example, we will be using the following 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” } ] As per the given data, the name of the student with the roll_number “1” is “elen”. The following query will update the student name to “Adam” − from tinydb import TinyDB, Query student = Query() db.update({”st_name” : ”Adam”}, student.roll_number == 1 ) It will return the id of the updated object − [1] Now, you can use the all() method to see the updated database − db.all() It will display the updated data − [ { “roll_number”:1, “st_name”:”Adam”, “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” } ] Sometimes, we need to update one or more fields of all the documents in a database. For this, we can use the update() mehod directly and don”t need to write the query argument. The following query will change the address of all the students to ”College_Hostel”− db.update({”address”: ”College_Hostel”}) It will return the ids of the updated object − [1,2,3,4,5] Again, you can use the all() method to see the updated database. db.all() It will show the updated data − [ { “roll_number”:1, “st_name”:”Adam”, “mark”:250, “subject”:”TinyDB”, “address”:”College_Hostel” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:” College_Hostel “ }, { “roll_number”:3, “st_name”:”kevin”, “mark”:[ 180, 200 ], “subject”:[ “oracle”, “sql” ], “address”:” College_Hostel “ }, { “roll_number”:4, “st_name”:”lakan”, “mark”:200, “subject”:”MySQL”, “address”:” College_Hostel “ }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”TinyDB”, “address”:” College_Hostel “ } ] Observe that the address fields of all the rows have the same data, i.e., ”College_Hostel”. Print Page Previous Next Advertisements ”;

TinyDB – Environmental Setup

TinyDB – Environmental Setup ”; Previous Next Prerequisite for TinyDB setup To install TinyDB, you must have Python 3.6 or newer installed in your system. You can go to the link www.python.org and select the latest version for your OS, i.e., Windows and Linux/Unix. We have a comprehensive tutorial on Python, which you can refer at www.tutorialspoint.com Installing TinyDB You can install TinyDB in three different ways: using the Pack Manager, from its Source, or from GitHub. Using the Package Manager The latest release versions of TinyDB are available over both the package managers, pip and conda. Let us check how you can use them to install TinyDB − To install TinyDB using pip, you can use the following command − pip install tinydb To install TinyDB via conda-forge, you can use the following command − conda install -c conda-forge tinydb From Source You can also install TinyDB from source distribution. Go to link https://pypi.org/project/tinydb/#files to download the files and building it from source. From GitHub To install TinyDB using GitHub, grab the latest development version, unpack the files, and use the following command to install it − pip install Setting up TinyDB Once installed, use the following steps to set up the TinyDB database. Step 1: Import TinyDB and its Query First, we need to import TinyDB and its Query. Use the following command − from tinydb import TinyDB, Query Step 2: Create a file TinyDB database can store data in many formats like XML, JSON, and others. We will be creating a JSON file by using the following file − db = TinyDB(”Leekha.json”) The above command will create an instance of TinyDB class and pass the file Leekha.Json to it. This is the file where our data will be stored. Now, the TinyDB database set up is ready, and you can work on it. We can now insert data and operate the value in the database. Uinstalling TinyDB If in case you need to uninstall TinyDB, you can use the following command − pip uninstall tinydb Print Page Previous Next Advertisements ”;

TinyDB – Insert Data

TinyDB – Insert Data ”; Previous Next We have created the instance of the TinyDB and passed a JSON file to it where our data will be stored. It is now time to insert the items in our database. The data should be in the form of a Python dictionary. Syntax To insert an item, you can use insert() method whose syntax is given below − db.insert({”type1”: ”value1”, ”type2”: ”value2”, ”typeN”: ”valueN”}) We can also create a dictionary first and then use the insert() method to insert the data into our database. data_item = {”type1”: ”value1”, ”type2”: ”value2”, ”typeN”: ”valueN” } db.insert(data_item) After running the above command, the insert() method will return the ID of the newly created object. And, our JSON file will look like the one shown below − {“_default”: {“1”: {“type1”: “value1”, “type2”: “value2”, “typeN”: “valueN”}}} Look at the above table entries: ”default” is the name of the table, ”1” is the ID of the newly created object, and the values are the data we have just inserted. Example: Inserting a Single Item Let”s understand the above concept with the help of examples. Suppose we have a database having student information showing roll numbers, names, marks, subjects, and addresses. Following is the information stored in the 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”:”oracle”, “address”:”benglore” } ] In the above database, if you want to insert a new student record (i.e., a single item), use the following command − db.insert({ ”roll_number”: 6, ”st_name”:”jim”, ”mark”:300, ”subject”:”sql”, ”address”:”pune” }) It will return the ID of the newly created object − 6 Let”s enter one more record− db.insert({ ”roll_number”: 7, ”st_name”:”karan”, ”mark”:290, ”subject”:”NoSQL”, ”address”:”chennai” }) It will return the ID of the newly created object − 7 If you want to check the stored items in the database, use the all() method as follows − db.all() It will produce the following output − [ { “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” }, { “roll_number”:6, “st_name”:”jim”, “mark”:300, “subject”:”sql”, “address”:”pune” }, { “roll_number”:7, “st_name”:”karan”, “mark”:290, “subject”:”NoSQL”, “address”:”chennai” } ] You can observe that it added two new data items in the JSON file. Example: Inserting Multiple items at a Time You can also insert multiple items at a time in a TinyDB database. For this, you need to use the insert_multiple() method. Let” see an example − items = [ {”roll_number”: 8, ”st_name”: ”petter”, ”address”: ”mumbai”}, {”roll_number”: 9, ”st_name”: ”sadhana”, ”subject”: ”SQL”} ] db.insert_multiple(items) Now, check the stored items in database, using the all() method as follows − db.all() It will produce the following output − [ { “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” }, { “roll_number”:6, “st_name”:”jim”, “mark”:300, “subject”:”sql”, “address”:”pune” }, { “roll_number”:7, “st_name”:”karan”, “mark”:290, “subject”:”NoSQL”, “address”:”chennai” }, { “roll_number”:8, “st_name”:”petter”, “address”:”mumbai” }, { “roll_number”:9, “st_name”:”sadhana”, “subject”:”SQL” } ] You can observe that it added two new data items in the JSON file. You can also skip some key values from the dataitems (as we have done) while adding the last two items. We have skipped ”mark” and ”address”. Print Page Previous Next Advertisements ”;

TinyDB – Delete Data

TinyDB – Delete Data ”; Previous Next In case you need to delete a particular set of data permanently from a TinyDB database, you can do so by using the remove() method. Like for reteriving and updating the data, you first need to create an instance of the Query class for deleting the data. You can use the following command for this purpose − from tinydb import Query Student = Query() Here, Student is the name of our database. The remove() Method Here is the syntax for the remove() method − db.remove( Query() field regex ) The remove() method accepts both an optional condition as well as an optional list of documents IDs. The student Database We will use the following student database, for the examples in this chapter. [ { “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” } ] Example: Deleting a Single Row of Data Let”s take an example to understand the remove() method. from tinydb import TinyDB, Query student = Query() db.remove(student.roll_number == 5) The above query will delete the data where the student”s roll number is 5. It will return the ID of the removed object − [5] Now, we can use the all() method to see the updated database. db.all() It will display the data from the updated database − [ { “roll_number”:1, “st_name”:”Adam”, “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” } ] Example: Deleting Multiple Rows of Data If you want to remove more than one row at a time, you can use the remove() method as follows − from tinydb import TinyDB, Query student = Query() db.remove(student.roll_number > 2) It will return the IDs of the removed object: [3,4] Use the all() method to see the updated database. db.all() It will display the data from the updated database − [ { “roll_number”:1, “st_name”:”Adam”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”delhi” } ] Example: Deleting the Entire Data If you want to remove all the data from a database, you can use the truncate() method as follows − db.truncate() Next, use the all() method to see the updated database. db.all() It will show an empty database as the output − [] Print Page Previous Next Advertisements ”;

TinyDB – Home

TinyDB Tutorial PDF Version Quick Guide Resources Job Search Discussion TinyDB is a lightweight database to operate various formats of the document. It is an easy and hustles free database to handle data of several applications. TinyDB is based on python code and supports clean API. This database does not need any coding language. It handles small projects without any configurations. Generally, a database can store, retrieve, and modify data in a JSON file. Audience TinyDB tutorial is helpful to learn from students to professionals in easy steps. This tutorial is designed for beginners to advance level developers for a web application. This tutorial makes you an intermediate or advanced level expert with practice. Prerequisites To learn this tutorial, you need to know about the Python version of your computer. You must know the working procedure of the command prompt. You do not need to learn coding language or install the software. Print Page Previous Next Advertisements ”;

TinyDB – Retrieve Data

TinyDB – Retrieve Data ”; Previous Next 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 implies, returns the list of items that mathches the query we provided, otherwise it will return an empty list. 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 like the all() method, it will retrieve all the rows from the student database. Print Page Previous Next Advertisements ”;