Peewee 简明教程

Peewee - Database Class

Peewee 包中的 Database 类的一个对象表示与数据库的连接。Peewee 通过 Database 类的相应子类,为 SQLite、PostgreSQL 和 MySQL 数据库提供了开箱即用的支持。

An object of Database class from Peewee package represents connection to a database. Peewee provides out-of-box support for SQLite, PostgreSQL and MySQL databases through corresponding subclasses of Database class.

Database 类的实例拥有打开与数据库引擎的连接所需的所有信息,并用于执行查询,管理事务和执行表的内省,列等。

Database class instance has all the information required to open connection with database engine, and is used to execute queries, manage transactions and perform introspection of tables, columns, etc.

Database 类拥有 SqliteDatabasePostgresqlDatabaseMySQLDatabase 子类。尽管用于 SQLite 的 DB-API 驱动器,采用 sqlite3 模块的形式,包含在 Python 的标准库中,但 psycopg2pymysql 模块必须先安装以便将 PostgreSql 和 MySQL 数据库与 Peewee 一起使用。

Database class has SqliteDatabase, PostgresqlDatabase and MySQLDatabase sub-classes. While DB-API driver for SQLite in the form of sqlite3 module is included in Python’s standard library, psycopg2 and pymysql modules will have to be installed first for using PostgreSql and MySQL databases with Peewee.

Using Sqlite Database

Python 在 sqlite3 模块的形式中拥有对 SQLite 数据库的内置支持。因此,它的连接非常容易。Peewee 中的 SqliteDatabase 类的对象表示连接对象。

Python has built-in support for SQLite database in the form of sqlite3 module. Hence, it is very easy to connect. Object of SqliteDatabase class in Peewee represents connection object.

con=SqliteDatabase(name, pragmas, timeout)

此处, pragma 是用于修改 SQLite 库操作的 SQLite 扩展。此参数可以是包含要每次打开连接时设置的 pragma 键值的一本词典或一个由 2-tuple 组成的列表。

Here, pragma is SQLite extension which is used to modify operation of SQLite library. This parameter is either a dictionary or a list of 2-tuples containing pragma key and value to set every time a connection is opened.

Timeout 参数以秒为单位指定,用以设置 SQLite 驱动器的繁忙超时。这两个参数都是可选的。

Timeout parameter is specified in seconds to set busy-timeout of SQLite driver. Both the parameters are optional.

下面的语句会创建一个新 SQLite 数据库的连接(如果它不存在)。

Following statement creates a connection with a new SQLite database (if it doesn’t exist already).

>>> db = peewee.SqliteDatabase('mydatabase.db')

Pragma 参数通常提供给新的数据库连接。pragmase 词典中提到的典型属性为 journal_modecache_sizelocking_modeforeign-keys 等。

Pragma parameters are generally given for a new database connection. Typical attributes mentioned in pragmase dictionary are journal_mode, cache_size, locking_mode, foreign-keys, etc.

>>> db = peewee.SqliteDatabase(
   'test.db', pragmas={'journal_mode': 'wal', 'cache_size': 10000,'foreign_keys': 1}
)

以下 pragma 设置是理想的且应指定 −

Following pragma settings are ideal to be specified −

Pragma attribute

Recommended value

Meaning

journal_mode

wal

allow readers and writers to co-exist

cache_size

-1 * data_size_kb

set page-cache size in KiB

foreign_keys

1

enforce foreign-key constraints

ignore_check_constraints

0

enforce CHECK constraints

Synchronous

0

let OS handle fsync

Peewee 还有一个 Another Python SQLite Wrapper (apsw) —— 一个高级 sqlite 驱动器。它提供虚拟表和文件系统以及共享连接等高级功能。APSW 比标准库 sqlite3 模块更快。

Peewee also has Another Python SQLite Wrapper (apsw), an advanced sqlite driver. It provides advanced features such as virtual tables and file systems, and shared connections. APSW is faster than the standard library sqlite3 module.