Category: tinydb

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

    TinyDB – The exists() Query



    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 −

    []
    

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

    TinyDB – Logical Negate



    Logical Negate works as an inverse logical gate. It will match the documents that don”t match the given query. In simple words, it will display the opposite meaning of the given command.

    Syntax

    The syntax of TinyDB Logical Negate is as follows −

    db.search(~(Query().field)
    

    Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student. It will fetch the data that represents the opposite meaning of the given command.

    Let”s take a couple of examples and see how it works. 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 student name is not ”elen” −

    from tinydb import TinyDB, Query
    db = TinyDB(''student.json'')
    db.search(~(Query().st_name == ''elen''))
    

    The above query will fetch all the rows where the student name is not “elen”.

    [
       {
          "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 avoid a particular address using logical negate −

    from tinydb import TinyDB, Query
    db = TinyDB(''student.json'')
    db.search(~(student.address.one_of([''keral'', ''delhi''])))
    

    This query will fetch all the rows where the “address” field does not have either “keral” or “delhi”.

    [
       {
          "roll_number":4,
          "st_name":"lakan",
          "mark":200,
          "subject":"MySQL",
          "address":"mumbai"
       },
       {
          "roll_number":5,
          "st_name":"karan",
          "mark":275,
          "subject":"TinyDB",
          "address":"benglore"
       }
    ]
    

    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 – The Matches() Query nhận dự án làm có lương

    Khóa học miễn phí TinyDB – The Matches() Query nhận dự án làm có lương


    TinyDB – The matches() Query



    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 −

    []
    

    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 – The Matches() Query: Cơ Hội Nhận Dự Án Làm Có Lương!

    Bạn đang tìm kiếm cơ hội để nâng cao kỹ năng truy vấn dữ liệu trong TinyDB và muốn làm việc trên các dự án thực tế có lương? Khóa học miễn phí TinyDB – The Matches() Query chính là cơ hội tuyệt vời dành cho bạn! Tham gia khóa học này không chỉ giúp bạn hiểu rõ cách sử dụng hàm matches() trong TinyDB mà còn mở ra cơ hội nhận các dự án thực tế có lương. Cùng khám phá chi tiết về khóa học và những lợi ích mà nó mang lại!

    khoa hoc mien phi TinyDB – The Matches() Query
    khoa hoc mien phi TinyDB – The Matches() Query

    Tại Sao Nên Tham Gia Khóa Học TinyDB – The Matches() Query?

    TinyDB là một cơ sở dữ liệu NoSQL nhẹ và dễ sử dụng, lý tưởng cho các ứng dụng nhỏ và nhúng. Hàm matches() trong TinyDB là một công cụ mạnh mẽ giúp bạn thực hiện các truy vấn dữ liệu dựa trên các điều kiện khớp với mẫu (pattern matching). Khóa học TinyDB – The Matches() Query sẽ cung cấp cho bạn kiến thức cần thiết để tận dụng hàm matches() để tối ưu hóa việc truy vấn dữ liệu một cách linh hoạt và hiệu quả.

    Khóa học này sẽ cung cấp cho bạn:

    • Kiến Thức Về Hàm matches(): Hiểu rõ vai trò và cách sử dụng hàm matches() để thực hiện các truy vấn dựa trên mẫu.
    • Kỹ Năng Truy Vấn Dữ Liệu: Học cách áp dụng hàm matches() để tìm kiếm và lọc dữ liệu theo các điều kiện phức tạp.
    • Thực Hành Tinh Thông: Áp dụng kiến thức vào các bài tập và dự án thực tế để củng cố kỹ năng.
    khoa hoc mien phi TinyDB – The Matches() Query
    khoa hoc mien phi TinyDB – The Matches() Query

    Nội Dung Khóa Học

    Khóa học TinyDB – The Matches() Query bao gồm các nội dung chính sau:

    1. Giới Thiệu về TinyDB: Tìm hiểu về cấu trúc cơ bản và các tính năng của TinyDB.
    2. Khái Niệm Về Hàm matches(): Định nghĩa và vai trò của hàm matches() trong việc thực hiện các truy vấn dữ liệu dựa trên mẫu.
    3. Sử Dụng Hàm matches(): Hướng dẫn chi tiết về cách sử dụng hàm matches() để thực hiện các truy vấn dữ liệu linh hoạt.
    4. Tối Ưu Hóa Truy Vấn: Cách tối ưu hóa việc sử dụng hàm matches() để cải thiện hiệu suất và khả năng mở rộng của hệ thống.
    5. Dự Án Thực Tế: Áp dụng các kiến thức vào các bài tập và dự án thực tế để nâng cao kỹ năng.
    khoa hoc mien phi TinyDB – The Matches() Query
    khoa hoc mien phi TinyDB – The Matches() Query

    Cơ Hội Nhận Dự Án Làm Có Lương

    Sau khi hoàn thành khóa học, bạn có cơ hội nhận các dự án thực tế có lương. Đây là một phần trong chương trình hỗ trợ học viên của chúng tôi, nhằm giúp bạn áp dụng kiến thức vào công việc thực tế và kiếm thêm thu nhập. Các dự án này sẽ được phân công dựa trên kỹ năng và sở thích của bạn, đảm bảo bạn có thể làm việc hiệu quả và phát triển sự nghiệp trong lĩnh vực quản lý cơ sở dữ liệu.

    Cách Đăng Ký

    Để tham gia khóa học TinyDB – The Matches() Query, bạn chỉ cần:

    1. Đăng Ký: Truy cập trang đăng ký trên website của chúng tôi và hoàn tất các bước đăng ký.
    2. Tham Gia Khóa Học: Theo dõi lịch trình học tập và hoàn thành các bài tập theo yêu cầu.
    3. Nhận Dự Án: Sau khi hoàn thành khóa học, bạn có thể đăng ký nhận các dự án làm việc có lương và bắt đầu thực hiện.
    khoa hoc mien phi TinyDB – The Matches() Query
    khoa hoc mien phi TinyDB – The Matches() Query

    Đăng Ký Ngay Để Không Bỏ Lỡ!

    Khóa học TinyDB – The Matches() Query hoàn toàn miễn phí và mở ra cơ hội để bạn nâng cao kỹ năng truy vấn dữ liệu và nhận các dự án thực tế có lương. Đừng bỏ lỡ cơ hội tuyệt vời này để phát triển sự nghiệp của bạn trong lĩnh vực quản lý cơ sở dữ liệu.

    https://www.youtube.com/watch?v=5I7XvTHG_mk&t=30s

    Hãy đăng ký ngay hôm nay và bắt đầu hành trình học tập và làm việc với TinyDB!


    Chúng tôi rất mong được đồng hành cùng bạn trong việc nâng cao kỹ năng và phát triển nghề nghiệp.

    Nếu bạn có bất kỳ câu hỏi nào hoặc cần thêm thông tin, vui lòng liên hệ với chúng tôi qua email hoặc trang hỗ trợ trên website.

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

    TinyDB – The one_of() Query



    For matching the subfield data, TinyDB provides a method called one_of(). This method searches a single category and gets at least one similar value. It will match if the field is contained in the provided list.

    Syntax

    The syntax of TinyDB one_of() is as follows −

    db.search(Query().field.one_of(list)
    

    Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student. It will fetch either single or multiple values of one category.

    Let”s understand how it works with the help of a couple 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 address is either “delhi” or “pune” −

    from tinydb import TinyDB, Query
    db = TinyDB(''student.json'')
    db.search(Query().address.one_of([''delhi'', ''pune'']))
    

    It will fetch all the rows where the “address” field contains either “delhi” or “pune”.

    [
       {
          "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

    Let”s see another example with ”subject” field −

    from tinydb import TinyDB, Query
    db = TinyDB(''student.json'')
    db.search(Query().subject.one_of([''TinyDB'', ''MySQL'']))
    

    It will fetch all the rows where the “subject” field contains either “TinyDB” or “MySQL”.

    [
       {
          "roll_number":1,
          "st_name":"elen",
          "mark":250,
          "subject":"TinyDB",
          "address":"delhi"
       },
       {
          "roll_number":4,
          "st_name":"lakhan",
          "mark":200,
          "subject":"MySQL",
          "address":"mumbai"
       },
       {
          "roll_number":5,
          "st_name":"karan",
          "mark":275,
          "subject":"TinyDB",
          "address":"benglore"
       }
    ]
    

    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 – The Any() Query nhận dự án làm có lương

    TinyDB – The any() Query



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


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