SQLAlchemy 核心 – 使用多个表
SQLAlchemy 核心 – 使用多个表
RDBMS 的重要特性之一是建立表之间的关系。可以对相关表执行 SELECT、UPDATE 和 DELETE 等 SQL 操作。本节使用 SQLAlchemy 描述这些操作。
为此,在我们的 SQLite 数据库 (college.db) 中创建了两个表。student 表的结构与上一节中给出的结构相同;而地址表具有st_id列,该列使用外键约束映射到学生表中的 id 列。
以下代码将在 College.db 中创建两个表 –
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, ForeignKey engine = create_engine('sqlite:///college.db', echo=True) meta = MetaData() students = Table( 'students', meta, Column('id', Integer, primary_key = True), Column('name', String), Column('lastname', String), ) addresses = Table( 'addresses', meta, Column('id', Integer, primary_key = True), Column('st_id', Integer, ForeignKey('students.id')), Column('postal_add', String), Column('email_add', String)) meta.create_all(engine)
上面的代码将转换为学生和地址表的 CREATE TABLE 查询,如下所示 –
CREATE TABLE students ( id INTEGER NOT NULL, name VARCHAR, lastname VARCHAR, PRIMARY KEY (id) ) CREATE TABLE addresses ( id INTEGER NOT NULL, st_id INTEGER, postal_add VARCHAR, email_add VARCHAR, PRIMARY KEY (id), FOREIGN KEY(st_id) REFERENCES students (id) )
以下屏幕截图非常清楚地展示了上述代码 –
这些表通过执行表对象的insert() 方法填充数据。要在学生表中插入 5 行,您可以使用下面给出的代码 –
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String engine = create_engine('sqlite:///college.db', echo = True) meta = MetaData() conn = engine.connect() students = Table( 'students', meta, Column('id', Integer, primary_key = True), Column('name', String), Column('lastname', String), ) conn.execute(students.insert(), [ {'name':'Ravi', 'lastname':'Kapoor'}, {'name':'Rajiv', 'lastname' : 'Khanna'}, {'name':'Komal','lastname' : 'Bhandari'}, {'name':'Abdul','lastname' : 'Sattar'}, {'name':'Priya','lastname' : 'Rajhans'}, ])
借助以下代码在地址表中添加行–
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String engine = create_engine('sqlite:///college.db', echo = True) meta = MetaData() conn = engine.connect() addresses = Table( 'addresses', meta, Column('id', Integer, primary_key = True), Column('st_id', Integer), Column('postal_add', String), Column('email_add', String) ) conn.execute(addresses.insert(), [ {'st_id':1, 'postal_add':'Shivajinagar Pune', 'email_add':'[email protected]'}, {'st_id':1, 'postal_add':'ChurchGate Mumbai', 'email_add':'[email protected]'}, {'st_id':3, 'postal_add':'Jubilee Hills Hyderabad', 'email_add':'[email protected]'}, {'st_id':5, 'postal_add':'MG Road Bangaluru', 'email_add':'[email protected]'}, {'st_id':2, 'postal_add':'Cannought Place new Delhi', 'email_add':'[email protected]'}, ])
请注意,addresses 表中的 st_id 列是指 Students 表中的 id 列。我们现在可以使用这个关系从两个表中获取数据。我们想从与addresses 表中的st_id 对应的students 表中获取name和lastname。
from sqlalchemy.sql import select s = select([students, addresses]).where(students.c.id == addresses.c.st_id) result = conn.execute(s) for row in result: print (row)
选择对象将有效地转换为以下 SQL 表达式,连接两个表的公共关系 –
SELECT students.id, students.name, students.lastname, addresses.id, addresses.st_id, addresses.postal_add, addresses.email_add FROM students, addresses WHERE students.id = addresses.st_id
这将产生从两个表中提取相应数据的输出,如下所示 –
(1, 'Ravi', 'Kapoor', 1, 1, 'Shivajinagar Pune', '[email protected]') (1, 'Ravi', 'Kapoor', 2, 1, 'ChurchGate Mumbai', '[email protected]') (3, 'Komal', 'Bhandari', 3, 3, 'Jubilee Hills Hyderabad', '[email protected]') (5, 'Priya', 'Rajhans', 4, 5, 'MG Road Bangaluru', '[email protected]') (2, 'Rajiv', 'Khanna', 5, 2, 'Cannought Place new Delhi', '[email protected]')