Python Peewee 库2024 年 8 月 29 日 | 阅读 15 分钟 Peewee 是一个基于 ORM (对象关系映射) 的 Python 库,它支持 SQLite、MySQL、PostgreSQL 和 Cockroach 数据库。在下面的教程中,我们将学习如何使用 Python 编程语言中的 Peewee 库插入新记录、删除记录、创建索引等等。此外,我们还将了解 Peewee 相关的连接管理、文本和二进制字段、子查询、过滤器等内容。 那么,让我们开始吧。 理解 Python 中的 Peewee 库
设置环境我们可以使用 pip 安装程序,如下所示,从 **PyPI** (Python 包索引的缩写) 安装 Peewee 的最新版本。 语法 此库的运行没有任何其他依赖项。它与 SQLite 一起使用,无需安装任何其他包,因为 sqlite3 模块已包含在标准库中。 但是,为了使用 MySQL 和 PostgreSQL,我们可能需要安装 DB-API 兼容的驱动程序模块——分别是 pymysql 和 pyscopg2。Cockroach 数据库通过一个随 Peewee 库默认安装的 playhouse 扩展进行处理。 使用 Peewee 理解映射模型映射到数据库表,字段映射到表列,实例映射到表行。 Peewee 使用 MySQLDatabase 用于 MySQL,PostgresqlDatabase 用于 PostgreSQL,SqliteDatabase 用于 SQLite。在下面的教程中,我们将使用 SQLite 数据库。 理解 Peewee 中的字段类型Peewee 模型中的字段类型定义了模型的存储类型。它们被转换为相应的数据库列类型。
上表列出了 Peewee 字段类型以及相应的 SQLite、PostgreSQL 和 MySQL 列类型。 使用 Peewee 定义模型在第一个示例中,我们将创建一个简单的数据库表。 示例 输出 sqlite> select * from notes; 1|Went to the Gym|2021-12-09 2|Went to the Cinema|2021-12-08 3|Watered the plants|2021-12-08 4|Listened to music|2021-12-09 说明 在上面的代码片段中,我们导入了所需的库。然后我们创建了一个名为 **'testSpace.db'** 的数据库。然后我们创建了一个 Peewee 模型类,并添加了一些字段。我们为数据库创建了数据库表,并创建了表。最后,我们插入了表中的条目并保存了数据库。 使用 Peewee 删除表我们可以使用 **drop_table()** 函数来删除表。让我们看下面的示例来演示这一点。 示例 说明 在上面的代码片段中,我们导入了所需的库。然后我们创建了一个名为 **'testSpace.db'** 的数据库。然后我们定义了一个名为 **'Notes'** 的 Peewee 模型类并创建了一个表。然后我们使用 **drop_table()** 函数删除了该表。 Peewee insert_many() 方法Peewee 库的 **insert_many()** 方法用于执行批量创建操作。 让我们看下面的例子来演示这一点。 示例 输出 sqlite> select * from notes; 1|Went to the Gym|2021-12-10 2|Went to the Cinema|2021-12-08 3|Watered the plants|2021-12-08 4|Listened to music|2021-12-10 5|Visited friends in the morning|2021-12-07 6|Worked on a Project|2021-12-10 7|Went to Shopping mall|2021-12-06 8|Listened to songs|2021-12-02 9|Watched Web series all day|2021-12-04 10|Watered the plants|2021-12-02 11|Walked for half an hour|2021-12-08 说明 在上面的代码片段中,我们导入了所需的库。然后我们创建了数据库和表。稍后,我们将需要填充到表中的数据定义为一个字典列表。然后我们执行了批量操作,并使用 atomic 方法将批量操作放入事务中。 使用 Peewee 选择所有实例Peewee 的 **select** 方法用于检索已定义模型的实例。让我们看下面的示例来演示这一点 示例 输出 $ python fetch_data.py SELECT "t1"."id", "t1"."text", "t1"."created" FROM "notes" AS "t1" Went to the Gym on 2021-12-10 Went to the Cinema on 2021-12-08 Watered the plants on 2021-12-08 Listened to music on 2021-12-10 Visited friends in the morning on 2021-12-07 Worked on a Project on 2021-12-10 Went to Shopping mall on 2021-12-06 Listened to songs on 2021-12-02 Watched Web series all day on 2021-12-04 Watered the plants on 2021-12-02 Walked for half an hour on 2021-12-08 说明 在上面的代码片段中,我们导入了所需的库。然后我们创建了数据库和表。然后我们使用 **select()** 函数从表中选择列。然后我们使用 **for** 循环遍历表中的每一行并打印出来。结果,程序为用户返回了条目。 使用 Peewee where 方法过滤数据Peewee 的 **where** 方法允许程序员根据指定的条件过滤数据。让我们看下面的示例来理解这一点 示例 输出 5 Visited friends in the morning on 2021-12-07 6 Worked on a Project on 2021-12-10 7 Went to Shopping mall on 2021-12-06 8 Listened to songs on 2021-12-02 9 Watched Web series all day on 2021-12-04 10 Watered the plants on 2021-12-02 11 Walked for half an hour on 2021-12-08 说明 在上面的代码片段中,我们导入了所需的库,并创建了数据库和表。然后我们使用 where 方法以及 **select()** 函数来过滤表中的数据。然后我们使用 **for** 循环遍历表的每一行并为用户打印数据。 Peewee 多个 where 表达式我们可以多次使用 **where** 方法并将它们组合起来生成一个新表达式,从而精确过滤过程。让我们看下面的示例来演示这一点 示例 输出 4 Listened to music on 2021-12-10 5 Visited friends in the morning on 2021-12-07 6 Worked on a Project on 2021-12-10 7 Went to Shopping mall on 2021-12-06 8 Listened to songs on 2021-12-02 9 Watched Web series all day on 2021-12-04 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。我们现在已经使用了 **where** 方法来指定限制,以精确地过滤数据。然后我们使用 **for** 循环遍历行并打印它们。 使用 Peewee 检索单个实例有两种方法可以用来选择单个实例;它们都使用了 **get()** 函数。 让我们看下面的示例来理解它的工作原理 示例 输出 1 Went to the Gym 2021-12-10 6 Worked on a Project 2021-12-10 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。为了检索单个实例,我们可以采用第一种方式,使用 **where** 方法以及 **get()** 函数,或者采用第二种方式,使用 **get()** 函数。两种方法都在上面的示例中显示。 使用 Peewee 选择列我们可以在 **select** 方法中指定要包含在查询中的列名。 这里有一个示例演示了这一点 示例 输出 [('Went to the Gym', datetime.date(2021, 12, 10)), ('Went to the Cinema', datetime.date(2021, 12, 8)), ('Watered the plants', datetime.date(2021, 12, 8))] 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **select()** 函数指定了要选择的列名。我们还包含了 **limit** 函数来限制要打印的条目数。 使用 Peewee 计算实例数我们可以使用 **count** 方法来计算表中模型实例的数量。让我们看下面的示例来演示这一点 示例 输出 11 7 说明 在上面的代码片段中,我们导入了所需的库,并创建了数据库和表。然后我们使用 **count()** 函数来计算条目的总数。然后我们使用 **where()** 函数指定条目的选择限制,然后再次使用 **count()** 函数来仅打印选定条目的数量。 使用 Peewee 显示 SQL 语句Peewee 库提供了 **sql** 方法,允许程序员生成 SQL 语句。 让我们看下面的示例来演示这一点 示例 输出 ('SELECT "t1"."id", "t1"."text", "t1"."created" FROM "notes" AS "t1" WHERE ("t1"."id" = ?)', [4]) 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **select()** 函数和 **where()** 来选择 **id = 4** 的条目。然后我们使用 **sql** 函数来打印该选择操作的 SQL 语句。 Peewee offset 和 limit 属性Peewee 还提供了 **offset** 和 **limit** 等多个属性,允许程序员定义实例的初始跳过数以及要在 **select** 函数中包含的实例数。 让我们看下面的例子来演示这一点: 示例 输出 4 Listened to music 2021-12-10 5 Visited friends in the morning 2021-12-07 6 Worked on a Project 2021-12-10 7 Went to Shopping mall 2021-12-06 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **offset** 和 **limit** 属性以及 **select()** 函数,以选择从第 4 个条目到接下来的三个条目的条目。 使用 Peewee 排序我们可以使用 Peewee **order_by** 函数来检索实例。让我们看下面的示例来演示这一点 示例 输出 Ascending order ******************************** Listened to songs 2021-12-02 Watered the plants 2021-12-02 Watched Web series all day 2021-12-04 Went to Shopping mall 2021-12-06 Visited friends in the morning 2021-12-07 Went to the Cinema 2021-12-08 Watered the plants 2021-12-08 Walked for half an hour 2021-12-08 Went to the Gym 2021-12-10 Listened to music 2021-12-10 Worked on a Project 2021-12-10 Descending order ******************************** Went to the Gym 2021-12-10 Listened to music 2021-12-10 Worked on a Project 2021-12-10 Went to the Cinema 2021-12-08 Watered the plants 2021-12-08 Walked for half an hour 2021-12-08 Visited friends in the morning 2021-12-07 Went to Shopping mall 2021-12-06 Watched Web series all day 2021-12-04 Listened to songs 2021-12-02 Watered the plants 2021-12-02 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **select** 函数和 **order_by** 方法来选择表中的条目,并按升序排列它们。然后我们使用 **for** 循环遍历每一行并打印它们。然后我们再次使用 **select** 函数以及 **order_by** 方法。但是,我们添加了 **desc()** 函数,以便按降序排列条目,并通过 **for** 循环将其打印给用户。 使用 Peewee 删除实例 Peewee 库提供了 **delete_by_id** 方法,允许程序员删除由其 ID 标识的实例。此函数返回已删除实例的数量。 让我们看下面的例子来演示这一点: 示例 输出 1 - Went to the Gym on 2021-12-10 2 - Went to the Cinema on 2021-12-08 4 - Listened to music on 2021-12-10 5 - Visited friends in the morning on 2021-12-07 6 - Worked on a Project on 2021-12-10 7 - Went to Shopping mall on 2021-12-06 8 - Listened to songs on 2021-12-02 9 - Watched Web series all day on 2021-12-04 10 - Watered the plants on 2021-12-02 11 - Walked for half an hour on 2021-12-08 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **delete_by_id()** 函数删除了 **ID = 3** 的条目。然后我们为用户打印了整个表。结果,**ID = 3** 的条目已成功从表中删除。 使用 Peewee 删除多个实例 为了删除多个实例,我们可以调用 Peewee **delete** 方法。此方法将返回成功删除的实例数。 让我们考虑以下说明相同内容的示例 示例 输出 7 instances deleted 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **delete()** 方法和 **where()** 方法删除了 ID 大于 3 的实例。结果,从表中删除了七个实例。 使用 Peewee 更新实例 我们可以使用 Peewee **update** 方法来更新实例。它返回成功更新的实例数。 这里有一个示例演示了这一点 示例 输出 No. of rows updated: 1 说明 我们在上面的代码片段中导入了所需的库,并创建了数据库和表。然后我们使用 **update()** 函数更新了 **ID = 2** 的实例的日期。 |
我们请求您订阅我们的新闻通讯以获取最新更新。