处理数据¶
Working with Data
在 使用事务和DBAPI 中,我们学习了如何与 Python DBAPI 及其事务状态进行交互的基础知识。然后,在 使用数据库元数据 中,我们学习了如何使用 MetaData
和相关对象在 SQLAlchemy 中表示数据库表、列和约束。在本节中,我们将结合上述两个概念,在关系数据库中创建、选择和操作数据。我们与数据库的交互 总是 以事务的形式进行,即使我们将数据库驱动程序设置为在幕后使用 自动提交。
本节的组成部分如下:
使用 INSERT 语句 - 为了将一些数据插入数据库,我们介绍并演示了 Core 的
Insert
构造。ORM 视角的 INSERT 在下一节 使用 ORM 进行数据操作 中描述。使用 SELECT 语句 - 本节将详细描述
Select
构造,这是 SQLAlchemy 中最常用的对象。Select
构造为 Core 和 ORM 中心应用程序发出 SELECT 语句,这两种用例将在此处描述。其他 ORM 用例也在后面的章节 在查询中使用关系 以及 ORM 查询指南 中提到。使用 UPDATE 和 DELETE 语句 - 完成数据的 INSERT 和 SELECT,本节将从 Core 视角描述
Update
和Delete
构造的使用。特定于 ORM 的 UPDATE 和 DELETE 同样在 使用 ORM 进行数据操作 部分中描述。
In 使用事务和DBAPI, we learned the basics of how to interact with the Python DBAPI and its transactional state. Then, in 使用数据库元数据, we learned how to represent database tables, columns, and constraints within SQLAlchemy using the MetaData
and related objects. In this section we will combine both concepts above to create, select and manipulate data within a relational database. Our interaction with the database is always in terms of a transaction, even if we’ve set our database driver to use autocommit behind the scenes.
The components of this section are as follows:
使用 INSERT 语句 - to get some data into the database, we introduce and demonstrate the Core
Insert
construct. INSERTs from an ORM perspective are described in the next section 使用 ORM 进行数据操作.使用 SELECT 语句 - this section will describe in detail the
Select
construct, which is the most commonly used object in SQLAlchemy. TheSelect
construct emits SELECT statements for both Core and ORM centric applications and both use cases will be described here. Additional ORM use cases are also noted in the later section 在查询中使用关系 as well as the ORM 查询指南.使用 UPDATE 和 DELETE 语句 - Rounding out the INSERT and SELECTion of data, this section will describe from a Core perspective the use of the
Update
andDelete
constructs. ORM-specific UPDATE and DELETE is similarly described in the 使用 ORM 进行数据操作 section.