import sqlalchemy from sqlalchemy.orm import declarative_base, sessionmaker, Session # declarative 声明式 from sqlalchemy import create_engine, Column, Integer, String # 创建数据引擎 HOST = "home.rogersun.cn" USER = "root" PWD = "Sxzgx1209" PORT = "3306" DB = "orm_sqlalchemy" URI = f"mysql+pymysql://{USER}:{PWD}@{HOST}:{PORT}/{DB}" engine = create_engine(URI, echo=True) # 创建session session = sessionmaker(engine) # with session # with Session(engine) as db_session: # session.begin() # try: # db_session.add("some_object") # db_session.add("some_other_object") # except: # db_session.rollback() # raise # else: # db_session.commit() # 生成orm基类 Base = declarative_base() class User(Base): __tablename__ = 'user'