您也可以使用 X DevAPI 来操作关系表。在 MySQL 中,每个关系表都关联特定的存储引擎。本节中的示例使用 InnoDB 表,这些表位于 world_x 模式中。You can also use X DevAPI to work with relational tables. In
MySQL, each relational table is associated with a particular
storage engine. The examples in this section use
InnoDB tables in the
world_x schema.
确认模式Confirm the Schema
要显示分配给 db 全局变量的模式,请执行 db。To show the schema that is assigned to the db
global variable, issue db.
mysql-py> db
<Schema:world_x>
如果返回的值不是 Schema:world_x,请按如下方式设置 db 变量:If the returned value is not Schema:world_x,
set the db variable as follows:
mysql-py> \use world_x
Schema `world_x` accessible through db.
显示所有表Show All Tables
要显示 world_x 模式中的所有关系表,请在 db 对象上使用 get_tables() 方法。To display all relational tables in the world_x
schema, use the get_tables() method on the
db object.
mysql-py> db.get_tables()
[
<Table:city>,
<Table:country>,
<Table:countrylanguage>
]
基本表操作Basic Table Operations
按表范围的基本操作包括:Basic operations scoped by tables include:
| 操作形式Operation form | 说明Description |
|---|---|
db. |
insert() 方法将一个或多个记录插入指定的表中。The insert() method inserts one or more records into the named table. |
db. |
select() 方法返回指定表中的部分或所有记录。The select() method returns some or all records in the named table. |
db. |
update() 方法更新指定表中的记录。The update() method updates records in the named table. |
db. |
delete() 方法从指定的表中删除一个或多个记录。The delete() method deletes one or more records from the named table. |
相关信息Related Information
有关更多信息,请参阅 处理关系表。See Working with Relational Tables for more information.
CRUD EBNF 定义 提供了完整的操作列表。CRUD EBNF Definitions provides a complete list of operations.
有关设置
world_x示例架构的说明,请参见 第 22.4.2 节,“下载并导入 world_x 数据库”。See Section 22.4.2, “Download and Import world_x Database” for instructions on setting up theworld_xschema sample.