还有一个重点需要下载
pip install sqlalchemy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple
from fastapi import FastAPI
import uvicorn
from student.urls import student_id_mysql
from sqlalchemy import create_engine,String,Integer,select ,asc
from sqlalchemy.orm import DeclarativeBase,sessionmaker,Mapped,mapped_column
class Base(DeclarativeBase):passengine = create_engine("mysql+pymysql://root:123456@localhost:3306/fastapi1",echo=True)
class StudentEntiry(Base):__tablename__ = "student"id:Mapped[int] = mapped_column(Integer,primary_key=True)name:Mapped[str] = mapped_column(String(128),unique=True,nullable=False)gender:Mapped[str] = mapped_column(String(10),nullable=False)Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)app = FastAPI()
app.include_router(student_id_mysql,prefix="/stu",tags=["获取所有学生信息接口"])
if __name__ == '__main__':uvicorn.run("main:app",port=8888,reload=True)
报错问题
把这里的mysql+mysqldb改成mysql+pymysql就可以了