Files
------/SQL.py
T

14 lines
838 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SQLAlchemy 是 Python 的 ORM 数据库框架,用来在 Python 代码里操作 MySQL 等数据库
# 1. Base:基类,所有模型类(班级、学生)都要继承它
# 2. Column:定义表里真实存在的字段(主键、字符串、外键),数据库会生成这一列
# 3. relationship:ORM 的对象关联,数据库不会生成这一列,只是用来对象之间互相查询(一对多、多对多)
# 4. db (Session 会话):和数据库的连接会话,所有增删改查都要通过 session
# 5. query():构造查询,搭配`.first()`、`.all()`执行查询
# Session(db)作用:所有增删改查都靠它
#
# - `db.add()`:把对象加入缓存
# - `db.commit()`:提交,数据写入数据库
# - `db.rollback()`:回滚,撤销本次操作(报错时用)
# - `db.close()`:关闭会话