TinyDB – The exists() Query ”; Previous Next TinyDB provides an advanced query called exists() that checks the existence of data in a JSON file. The exists() query actually tests the availability of a subfield data from a JSON file. The exists() query works on the basis of a Boolean condition. If the subfield exists (i.e., BOOLEAN TRUE), it will fetch the data accordingly from the JSON file, otherwise it will return a blank value. Syntax The syntax of TinyDB exists() is as follows − db.search(Query().field.exists()) Here, field represents the part of data that we want to access. Query() is the object created from the JSON table student. We will use the same student database that we have used in all the previous chapters. Example 1 Let”s use the TinyDB exists() query for the field named ”subject” − db.search(student.subject.exists()) This query will fetch all the rows because all the rows have the “subject” field − [ { “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 Now let”s use the exists() query for the ”address” field − db.search(student.address.exists()) It will fetch the following 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 3 Now, let”s try the exists() query for a field that is not available − db.search(student.city.exists()) Since none of the rows in the given table has a field called “city”, the above exists() query will return a blank value − [] Print Page Previous Next Advertisements ”;
Category: tinydb
TinyDB – The Any() Query
TinyDB – The any() Query ”; Previous Next For searching the fields containing a list, TinyDB provides a method called any(). This method matches at least one given value from the database. It finds either an entire list or a minimum one value as per the query provided. Syntax The syntax of TinyDB any() is as follows − db.search(Query().field.any(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 query as the argument of any() method, it will match all the documents where at least one document in the list field match the given query. On the other hand, if we will provide list as the argument of any() method, it will match all the documents where at least one document in the list field is 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 subject is either TinyDB, or MySQL, or SQL or combination of any two or three − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.search(Query().subject.any([”TinyDB”, ”MySQL”, ”oracle”])) The above query will fetch all the rows where the “subject” field contains any of the following values: “TinyDB”, “MySQL”, or “oracle” − [ { “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” } ] Example 2 Let”s see how the any() method reacts when it doesn”t match anything from the given list − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.search(Query().subject.any([”Oracle”])) This query will return a blank value because there are no rows with its “subject” as “Oracle”. [] Example 3 Observe that it is case-sensitive. The “subject” field does not have “Oracle“, but it does have “oracle“. Try the following query − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.search(Query().subject.any([”oracle”])) It will fetch the following row − [{ ”roll_number”: 3, ”st_name”: ”kevin”, ”mark”: [180, 200], ”subject”: [”oracle”, ”sql”], ”address”: ”keral” }] As it is case-sensitive, it returned a blank value in the previous example because there are no rows with its “subject” as “Oracle”. Print Page Previous Next Advertisements ”;
TinyDB – Introduction
TinyDB – Introduction ”; Previous Next What is TinyDB? TinyDB, written in pure Python programming language, is a small and lightweight document-orineted database with no external dependencies. It provides simple APIs that makes it easy to use. We can use TinyDB database for small project applications without any configuration. TinyDB module, available as a third-party module for Python programs, can be used to store, retereive, and modify the data in JSON format. Features of TinyDB TinyDB is a clean and hustle-free database to operate several formats of documents. It has the following features. Really tiny − TinyDB database is truly tiny in nature with only 1800 lines of code and 1600 lines tests. Easy to use − TinyDB is easy to use because of its simple and clean APIs. Document oriented − In TinyDB, we can store any document. The document will be represented as dict. Independent − The TinyDB database is independent of any external server and external dependencies from PyPI. Compatible with Python 3.6 or latest − TinyDB is tested and compatible with Python 3.6 and latest. It also works fine with PyPY3. Extensible − TinDB is easily extensible either by writing new storages or by modifying the behaviour of storages. Advantages of TinyDB TinyDB provide various benefits for students, users, and developers. TinyDB is open-sourced database aand it does not require any external configirations. It is quite easy-to-use, and the user can effortlessly handle documents. It automatically stores documents in the database. TinyDB is ideal in case of personal projects where we need to install some data. It is suitable for small applications that would be blown away by large databases like SQL or an external DB server. It uses a simple command line and query to operate data. There is 100% test coverage i.e., no explanation needed. Limitatations of TinyDB TinyDB will not be the right choice for your project if you need to − create indexes for tables, manage relationships between tables, use an HTTP server, or access from multiple processors. Comparison with Other Databases The following table highlights how TinyDB is different from MySQL and Oracle databases − Comparison Basis MySQL Oracle TinyDB Configurations Several Configurations Several Configurations Less Configurations, lightweight database Complicated Yes Yes No, easy-to-use and hustle-free Affordable No No Affordable than other databases Manageable Big database, hence difficult to manage Big database, hence difficult to manage Small and manageable Print Page Previous Next Advertisements ”;
TinyDB – Querying
TinyDB – Querying ”; Previous Next TinyDB has a rich set of queries. We have ways to construct queries: the first way resembles the syntax of ORM tools and the second is the traditional way of using the ”Where” clause. In this chapter, let”s understand these two ways of constructing a query in a TinyDB database. The First Method: Importing a Query The first method resembles the syntax of ORM tools in which first we need to import the query in the command prompt. After importing, we can use the query object to operate the TinyDB database. The syntax is given below − from tinydb import Query student = Query() Here, ”student” is the name of our database. For the 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 Following is the query to retereive the data from the student database where the roll_no of the students are less than 3 − >>> db.search(Query().roll_number < 3) Or, >>> student = Query() >>> db.search(student.roll_number < 3) The above search query will produce the following result − [ { “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” } ] Sometimes the file name is not a valid Python identifier. In that case, we would not be able to access that field. For such cases, we need to switch to dict access notation as follows − student = Query(); # Invalid Python syntax db.search(student.security-code == ”ABCD”) # Use the following dict access notation db.search(student[”security-code”] == ”ABCD”) The Second Method: Using the “where” Clause The second way is the traditional way of constructing queries that uses the “where” clause. The syntax is given below − from tinydb import where db.search(where(”field”) == ”value”) Example TinyDB “where” clause for the subject field − db.search(where(”subject”) == ”MySQL”) The above query will produce the following output − [{ “roll_number”:4, “st_name”:”lakan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }] Print Page Previous Next Advertisements ”;