# 1. 导入SQLAlchemy from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String # 2.ORM 模型基类 Base = declarative_base() # 相当于Django model # 3. 创建ORM对象 class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(32), index=True) # 4.创建数据库连接 from sqlalchemy import create_engine engine = create_engine('mysql+pymysql://root:root@127.0.0.1:3306/my_sqlalchemy?charset=utf8') # 5. # 去engine数据库中创建所有继承Base类的ORM对象 Base.metadata.create_all(engine)