为后端添加上传学生信息功能

master
cflsxjw 2 months ago
parent 4ee7299f8f
commit 9a0fdbb7c1

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="BOOK_DEMO@localhost" uuid="062a74a1-344c-4cae-a936-47b5e67c1332">
<driver-ref>mariadb</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mariadb://localhost:3306/BOOK_DEMO</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

@ -1,36 +1,51 @@
from math import floor
from random import uniform
from fastapi import FastAPI
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import sqlalchemy
from sqlalchemy import text
import pandas as pd
app = FastAPI()
origins = ["http://localhost:5173"]
# noinspection PyTypeChecker
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
allow_headers=["*"]
)
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/BOOK_DEMO")
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://cflsxjw:1212@127.0.0.1:3306/ROLLER_DB")
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
@app.get("/get_random_stu")
@app.get("/api/get/get_random_stu")
async def get_random_stu():
with engine.connect() as c:
stu = c.execute(text("SELECT sno, sname FROM stu_info")).fetchall()
size = stu.__len__()
return {"sno": stu[floor(uniform(0, size - 1))][0], "sname": stu[floor(uniform(0, size - 1))][1]}
@app.get("/api/get/get_random_event")
async def get_event():
return {"eventId": 10, "eventDescription": "疯狂星期四!/v我50:P"}
@app.post("/api/post/upload_stu_info")
async def upload_stu_info(file: UploadFile):
upload_file = file.file
data = pd.read_csv(upload_file)
with engine.connect() as c:
c.execute(text("TRUNCATE TABLE stu_info"))
for i in data.index.values:
with engine.begin() as c:
c.execute(text(f"INSERT INTO stu_info VALUES ('{data.values[i, 0]}', '{data.values[i,1]}', 0);"))
return file.filename
Loading…
Cancel
Save