Author: alien

  • 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 – Insert Data nhận dự án làm có lương

    TinyDB – Insert Data



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


    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 – Querying nhận dự án làm có lương

    TinyDB – Querying



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

    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 – Update Data nhận dự án làm có lương

    TinyDB – Update Data



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


    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 – Home nhận dự án làm có lương

    TinyDB Tutorial

    TinyDB Tutorial







    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.

    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 – Delete Data nhận dự án làm có lương

    TinyDB – Delete Data



    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

    []
    

    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í T-SQL – DELETE Statement nhận dự án làm có lương

    T-SQL – DELETE Statement



    The SQL Server DELETE Query is used to delete the existing records from a table.

    You have to use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.

    Syntax

    Following is the basic syntax of DELETE query with WHERE clause −

    DELETE FROM table_name
    WHERE [condition];
    

    You can combine N number of conditions using AND or OR operators.

    Example

    Consider the CUSTOMERS table having the following records −

    ID  NAME       AGE       ADDRESS            SALARY
    1   Ramesh     32        Ahmedabad          2000.00
    2   Khilan     25        Delhi              1500.00
    3   kaushik    23        Kota               2000.00
    4   Chaitali   25        Mumbai             6500.00
    5   Hardik     27        Bhopal             8500.00
    6   Komal      22        MP                 4500.00
    7   Muffy      24        Indore             10000.00
    

    Following command is an example, which would DELETE a customer, whose ID is 6 −

    DELETE FROM CUSTOMERS
    WHERE ID = 6;
    

    CUSTOMERS table will now have the following records.

    ID  NAME       AGE       ADDRESS              SALARY
    1   Ramesh     32        Ahmedabad            2000.00
    2   Khilan     25        Delhi                1500.00
    3   kaushik    23        Kota                 2000.00
    4   Chaitali   25        Mumbai               6500.00
    5   Hardik     27        Bhopal               8500.00
    7   Muffy      24        Indore               10000.00
    

    If you want to DELETE all the records from CUSTOMERS table, you do not need to use WHERE clause. DELETE query would be as follows −

    DELETE FROM CUSTOMERS;
    

    CUSTOMERS table now will not have any record.


    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í T-SQL – LIKE Clause nhận dự án làm có lương

    T-SQL – LIKE Clause



    The MS SQL Server LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator −

    • The percent sign (%)
    • The underscore (_)

    The percent sign represents zero, one, or multiple characters. The underscore represents a single number or character. The symbols can be used in combinations.

    Syntax

    Following is the basic syntax of % and _.

    SELECT *column-list FROM table_name
    WHERE column LIKE ''XXXX%''
    
    or
    
    SELECT *column-list FROM table_name
    WHERE column LIKE ''%XXXX%''
    
    or
    
    SELECT *column-list FROM table_name
    WHERE column LIKE ''XXXX_''
    
    or
    
    SELECT *column-list FROM table_name
    WHERE column LIKE ''_XXXX''
    
    or
    
    SELECT  *column-list FROM table_name
    WHERE column LIKE ''_XXXX_''
    

    You can combine N number of conditions using AND or OR operators. XXXX could be any numeric or string value.

    Example

    Following are a number of examples showing WHERE part having different LIKE clause with ”%” and ”_” operators.

    Sr.No Statement & Description
    1

    WHERE SALARY LIKE ”200%”

    Finds any values that start with 200

    2

    WHERE SALARY LIKE ”%200%”

    Finds any values that have 200 in any position

    3

    WHERE SALARY LIKE ”_00%”

    Finds any values that have 00 in the second and third positions

    4

    WHERE SALARY LIKE ”2_%_%”

    Finds any values that start with 2 and are at least 3 characters in length

    5

    WHERE SALARY LIKE ”%2”

    Finds any values that end with 2

    6

    WHERE SALARY LIKE ”_2%3”

    Finds any values that have a 2 in the second position and end with a 3

    7

    WHERE SALARY LIKE ”2___3”

    Finds any values in a five-digit number that start with 2 and end with 3

    Consider the CUSTOMERS table having the following records.

    ID  NAME       AGE       ADDRESS            SALARY
    1   Ramesh     32        Ahmedabad          2000.00
    2   Khilan     25        Delhi              1500.00
    3   kaushik    23        Kota               2000.00
    4   Chaitali   25        Mumbai             6500.00
    5   Hardik     27        Bhopal             8500.00
    6   Komal      22        MP                 4500.00
    7   Muffy      24        Indore             10000.00
    

    Following command is an example, which will display all the records from CUSTOMERS table where SALARY starts with 200.

    SELECT * FROM CUSTOMERS
    WHERE SALARY LIKE ''200%
    

    The above command will produce the following output.

    ID   NAME     AGE     ADDRESS       SALARY
    1    Ramesh   32      Ahmedabad     2000.00
    3    kaushik  23      Kota          2000.00
    

    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