Peewee 简明教程

Peewee - Overview

Peewee 是一个 Python 对象关系映射(ORM)库,由一位位于美国软件工程师 Charles Leifer 开发于 2010 年 10 月。其最新版本是 3.13.3. Peewee 支持 SQLite、MySQL、PostgreSQL 和 Cockroach 数据库。

Peewee is a Python Object Relational Mapping (ORM) library which was developed by a U.S. based software engineer Charles Leifer in October 2010. Its latest version is 3.13.3. Peewee supports SQLite, MySQL, PostgreSQL and Cockroach databases.

对象关系映射是一种编程技术,用于在面向对象编程语言中将数据在不兼容类型系统之间进行转换。

Object Relational Mapping is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

在面向对象(OO)编程语言中(例如 Python),按定义的类被认为是非标量的。它不能表示为整数和字符串等基元类型。

Class as defined in an Object Oriented (OO) programming language such as Python, is considered as non-scalar. It cannot be expressed as primitive types such as integers and strings.

另一方面,Oracle、MySQL、SQLite 等数据库只能存储和处理表内组织的整数和字符串等标量值。

On the other hand, databases like Oracle, MySQL, SQLite and others can only store and manipulate scalar values such as integers and strings organised within tables.

程序员必须将对象值转换为标量数据类型的组,以便存储在数据库中,或者在检索后将它们转换回标量值,或仅在程序内使用简单的标量值。

The programmer must either convert the object values into groups of scalar data types for storage in the database or convert them back upon retrieval, or only use simple scalar values within the program.

在 ORM 系统中,每个类都映射到基础数据库中的一个表。你无需自己编写繁琐的数据库接口代码, ORM 会处理这些问题,而你可以专注于对系统逻辑进行编程。

In an ORM system, each class maps to a table in the underlying database. Instead of writing tedious database interfacing code yourself, an ORM takes care of these issues, while you can focus on programming the logics of the system.

Environment setup

要安装 Peewee 的最新版本(由 PyPI(Python 包索引)托管),请使用 pip 安装程序。

To install latest version of Peewee as hosted on PyPI (Python Package Index), use pip installer.

pip3 install peewee

Peewee 工作没有任何其他依赖项。它不用安装任何其他包即可使用 SQLite,因为 sqlite3 模块与标准库捆绑在一起。

There are no other dependencies for Peewee to work. It works with SQLite without installing any other package as sqlite3 module is bundled with standard library.

但是,要使用 MySQL 和 PostgreSQL,你可能不得不分别安装 DB-API 兼容驱动器模块 pymysql 和 pyscopg2。Cockroach 数据库通过 playhouse 扩展来处理,该扩展将随 Peewee 一起默认安装。

However, to work with MySQL and PostgreSQL, you may have to install DB-API compatible driver modules pymysql and pyscopg2 respectively. Cockroach database is handled through playhouse extension that is installed by default along with Peewee.

Peewee 是托管在 https://github.com/coleifer/peewee 存储库上的开源项目。因此,它可以通过使用 Git 从这里安装。

Peewee is an open source project hosted on https://github.com/coleifer/peewee repository. Hence, it can be installed from here by using git.

git clone https://github.com/coleifer/peewee.git
cd peewee
python setup.py install