In TinyDB, we can work with multiple tables. These tables have the same properties as the TinyDB class. Let's see how we can create tables in TinyDB and apply various operations on them −
Creating Tables : It’s very easy to create a table in TinyDB. Here's its syntax −
table_object = db.table('table name')?
Inserting Values in a Table : To insert data in a specific table, use the following syntax −
table_object.insert({ 'key' : value })?
Retreiving Values from a Table : To retrieve values from a table, use the following query −
table_object.all()?
Deleting a Table from a Database : To delete a table from a database, use the drop_table() query. Here is its syntax −
db.drop_table('table name')?
Delete Multiple Tables from a Database : To delete multiple tables from a database, use the following query −
db.drop_tables()?
Let's understand how to use these queries with the help of a few examples. We will use the same student database that we have used in all the previous chapters.
Example 1 : Use the following code to create a new table called Student_Detail −
from tinydb import TinyDB, Query
db = TinyDB("student.json")
table_object = db.table('Student_Detail')?