Category: tinydb

  • Khóa học miễn phí TinyDB – Introduction nhận dự án làm có lương

    TinyDB – Introduction



    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

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí TinyDB – Retrieve Data nhận dự án làm có lương

    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 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.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí TinyDB – Environmental Setup nhận dự án làm có lương

    TinyDB – Environmental Setup



    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 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

    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 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
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí TinyDB – Searching nhận dự án làm có lương

    TinyDB – Searching



    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''
    }]
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc