20 lines
543 B
Python
20 lines
543 B
Python
from sqlalchemy import *
|
|
from sqlalchemy.orm import declarative_base,sessionmaker
|
|
|
|
db_url = "mysql+pymysql://root:123456@127.0.0.1:3306/student?charset=utf8mb4" #创建连接对象
|
|
engine = create_engine(db_url) #与数据库进行连接
|
|
|
|
Base = declarative_base() # 执行函数,返回一个基类
|
|
|
|
|
|
Session = sessionmaker(bind=engine
|
|
,autoflush=False
|
|
,autocommit = False
|
|
)
|
|
|
|
def get_db():
|
|
db = Session()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |