Tinydb 简明教程

TinyDB - Tables

在 TinyDB 中,我们可以使用多张表。这些表的属性与 TinyDB 类相同。让我们看看如何在 TinyDB 中创建表并在其上应用各种操作 −

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

在 TinyDB 中创建表非常容易。以下是它的语法 −

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

若要从数据库中删除表,请使用 drop_table() 查询。以下是其 syntax

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

让我们借助一些示例了解如何使用这些查询。我们将使用我们在所有前几章中使用的相同 student 数据库。

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

使用以下代码创建名为 Student_Detail 的新表 −

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

Example 2

接下来,让我们在这个新表 Student_Detail 中插入值 −

Next, let’s insert values in this new table Student_Detail

from tinydb import TinyDB, Query
db = TinyDB("student.json")
table_object = db.table('Student_Detail')
table_object.insert({
   'roll_number': 1,
   'st_name': 'elen',
   'mark': 250,
   'subject': 'TinyDB',
   'address': 'delhi'
})

它将返回在表中插入的记录的 doc_id。

It will return the doc_id of the record inserted in the table.

[1]

要进行验证,请使用以下代码:

To verify, use the following code −

from tinydb import TinyDB, Query
db = TinyDB("student.json")
table_object = db.table('Student_Detail')
table_object.all()

它将显示 Student_Detail 表包含的数据:

It will show data contained in the Student_Detail table −

{'roll_number': 1, 'st_name': 'elen', 'mark': 250, 'subject': 'TinyDB', 'address': 'delhi'}

Example 3

要查看数据库中存在的所有表,请使用以下查询:

To see all the tables present in the database, use the following query −

from tinydb import TinyDB, Query
db = TinyDB("student.json")
print(db.tables())

"student.json" 中有两个表。它将显示这两个表的名称:

There are two tables inside "student.json". It will show the names of these two tables −

{'Student_Detail', '_default'}

Example 4

让我们看看如何从表中检索所有值:

Let’s see how we can reterive all the values from a table −

from tinydb import TinyDB, Query
db = TinyDB("student.json")
table_object = db.table("Student_Detail")
print(table_object.all())

它将显示以下内容 output

It will show the following output

[{
   'roll_number': 1,
   'st_name': 'elen',
   'mark': 250,
   'subject': 'TinyDB',
   'address': 'delhi'
}]

Example 5

让我们看看如何从数据库中删除一个表:

Let’s see how we can remove a table from a database −

from tinydb import TinyDB, Query
db = TinyDB("student.json")
db.drop_table('Student_Detail')

它将从数据库中删除 "Student_Detail" 表。要从数据库中删除所有表,请使用 "drop_tables()" 查询:

It will remove the "Student_Detail" table from the database. To remove all the tables from a database, use the "drop_tables()" query −

db.drop_tables()

它将从数据库中删除所有表。

It will remove all the tables from the database.