当前位置: 首页 > news >正文

网站架构师招聘seo方法图片

网站架构师招聘,seo方法图片,网站怎么做自营销,网页制作图片显示不出来sqlalchemy FastAPI 前端实现数据库增删改查 仅个人学习笔记,感谢点赞关注! 知识点 连接数据库sqlalchemy 创建表结构FastAPI get post put delete操作FastAPI 请求体 路径和修改参数 依赖项 代码 # -*- ecoding: utf-8 -*- # Author: SuperLong # Em…

sqlalchemy FastAPI 前端实现数据库增删改查

仅个人学习笔记,感谢点赞关注!


知识点
  • 连接数据库
  • sqlalchemy 创建表结构
  • FastAPI get post put delete操作
  • FastAPI 请求体 路径和修改参数 依赖项

代码
# -*- ecoding: utf-8 -*-
# @Author: SuperLong
# @Email: miu_zxl@163.com
# @Time: 2024/9/9 17:04
import os
import uvicorn
from fastapi import FastAPI, Depends, HTTPException, Path,status
from pydantic import BaseModel
from typing import List,Optional,Set
from sqlalchemy import create_engine, Column, Integer, String, and_, select, update
from sqlalchemy.orm import sessionmaker, Mapped, DeclarativeBase, mapped_columnengine = create_engine('mysql://root:long520@localhost/test',echo=True)
class Base(DeclarativeBase):pass
class StudentClass(Base):__tablename__ = "StudentClass"id:Mapped[str]=mapped_column(Integer,primary_key=True)name:Mapped[str]=mapped_column(String(50),nullable=False)gender:Mapped[str]=mapped_column(String(5),nullable=False)Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()class StudentBase(BaseModel):id:intname:strgender:strclass StudentIn(StudentBase):passclass StudentOut(StudentBase):passdef get_db():db = Session()try:yield dbfinally:db.close()app = FastAPI()@app.get('/students')
async def get_students(db:Session=Depends(get_db)):query = select(StudentClass).order_by(StudentClass.id)return db.execute(query).scalars().all()@app.post('/students',response_model=StudentOut)
async def create_students(student:StudentIn,db:Session=Depends(get_db)):query = select(StudentClass).where(StudentClass.name == student.name)result = db.execute(query).scalars().all()if result:raise HTTPException(status_code=400,detail=f"学生 {student.name} 已存在")new_student = StudentClass(id=student.id,name=student.name,gender=student.gender)db.add(new_student)db.commit()return new_student@app.put('/students/{student_id}',response_model=StudentOut)
async def update_students(*,student_id:int=Path(...),student:StudentBase,db:Session=Depends(get_db)):query = select(StudentClass).where(StudentClass.id == student_id)result = db.execute(query).scalar()if not result:raise HTTPException(status_code=400, detail=f"学生ID {student_id} 不存在")def update_mm(students:dict,changes:dict):for keys,values in changes.items():setattr(students,keys,values)update_mm(result,student.model_dump())db.commit()return result@app.delete('/students/{student_id}',response_model=StudentOut)
def delete_students(student_id:int=Path(...),db:Session=Depends(get_db)):query = select(StudentClass).where(student_id == StudentClass.id)result = db.execute(query).scalar()if not result:raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"学生ID {student_id} 不存在")db.delete(result)db.commit()return resultif __name__ == '__main__':print(os.path.split(os.path.abspath(__file__))[1])uvicorn.run(port=5025,app=f"{os.path.split(os.path.abspath(__file__))[1].split('.')[0]}:app",reload=True)# # todo 增
# students = [
#             StudentClass(id=1, name="张",gender="男",phone_number="13463135455"),
#             StudentClass(id=2, name="张龍",gender="男",phone_number="13463125455"),
#             StudentClass(id=3, name="张晓同",gender="男",phone_number="13463145455"),
#             StudentClass(id=4, name="张晓里",gender="男",phone_number="13463165455"),
# ]
# session.add_all(students)
# session.commit()
# # todo 查
# result = session.query(StudentClass).filter(StudentClass.gender == "男").all()
# for ii in result:
#     print("name:",ii.id)
#     print("brithday:",ii.name)# todo 改
# result = session.query(StudentClass).filter(
#     and_(
#     StudentClass.gender == "男",
#     StudentClass.name == "李楠"
#     )
# ).update(
#     {StudentClass.phone_number:"123456789"}
# )
#
# session.commit()
# todo 删
# result = session.query(StudentClass).filter(
#     and_(
#         StudentClass.gender == "男",
#         StudentClass.name == "李佳"
#     )
# ).delete()
# session.commit()

目前专注于NLP、大模型和前后端的技术学习和分享

感谢大家的关注与支持!
http://www.ds6.com.cn/news/100938.html

相关文章:

  • 宽带哪家好李勇seo的博客
  • 专业网站建设制作价格媒体吧软文平台
  • 网站建设阶段搜索引擎营销的方法包括
  • 烟台市建设工程检测站网站网站关键词优化排名技巧
  • 武汉北京网站建设西安百度seo
  • 网站服务公司案例常州网络推广平台
  • 苏州网站关键字优化电商培训机构推荐
  • 四川遂宁做网站的公司搜索引擎推广实训
  • 国外设计网址国内专业seo公司
  • 网站建设信息服务费计入什么科目市场推广外包团队
  • 自己做网站怎么让字体居中如何引流被动加好友微信
  • dw网页制作代码大全可复制seo技术培训
  • dw做网站投票2023网络营销成功案例
  • h5手机网站制作aso排名服务公司
  • 如何套用别人网站模板小程序开发平台有哪些
  • 产品展示网站含后台网站模板下载正规app推广
  • 鞍山网站建设西安seo优化培训
  • 安阳 网站建设专业推广引流团队
  • 做旅游的网站的目的和意义seo优化教程视频
  • 郑州网站设计汉狮网络网站站点
  • 长春火车站封闭了吗有没有免费的广告平台
  • 温州做网站优化专业技能培训机构
  • 不锈钢网站建设青岛网络优化费用
  • 网站风格网站seo排名优化工具
  • 建站abc网站案例com天堂网
  • 郑州易站通网站公司湖北网络推广有限公司
  • 设计网站草图360搜索首页网址是多少
  • 深圳模板建站多少钱店铺推广软文300字
  • 青岛做网站的公司排名爱站网 关键词挖掘工具站
  • 广州哪里做网站橘子seo