From fd16ae3bed336a0e49fef049e5836cd745c72142 Mon Sep 17 00:00:00 2001 From: 1 <1@1> Date: Sat, 19 Sep 2026 14:50:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=E3=80=8C/=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | Bin 0 -> 1024 bytes database.py | 31 +++++++++++++++++++++++++++++++ main.py | Bin 0 -> 1024 bytes 3 files changed, 31 insertions(+) create mode 100644 __init__.py create mode 100644 database.py create mode 100644 main.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/database.py b/database.py new file mode 100644 index 0000000..33ad37d --- /dev/null +++ b/database.py @@ -0,0 +1,31 @@ +from sqlalchemy import * +from sqlalchemy.orm import declarative_base, sessionmaker + +db_url = "mysql+pymysql://root:123456@127.0.0.1:3306/ai0824?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() + +@order_api.put('/{id}') +def get_orders(order:OrderRequest,id:int,db=Depends(get_db)): + try: # 数据更新是有返回值的,返回的是影响的行数 + rows = db.query( Order ).filter( Order.id == id ).update( order.model_dump(exclude_unset=True) ) + except: + db.rollback() + raise HTTPException(status_code=500, detail='更新异常,请稍后执行!') + else: + db.commit() + print('更新影响的行数:',rows) + return {'code':200,'totals':rows,'detail':'更新成功'} diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001