from flask import Flask from pymongo import MongoClient app = Flask(__name__) client = MongoClient("mongodb://localhost:27019/") db = client["back"] collection = db["users"] # 初始化数据 def initData(): isAdmin = collection.find_one({"isAdmin": 1, "isExit": 1}) if isAdmin is None: # 学校名称、学校代号、专业名称、专业代号、年级、班级、学生姓名、学号、成绩、作弊情况(作弊类型、作弊时间、作弊图片)、考试类型、考试科目、考试时间段、是否是管理员、是否删除、创建时间、更新时间 # xuexiaomingcheng、xuexiaodaihao、zhuanyemingcheng、zhuanyedaihao、nianji、banji、xueshengxingming、xuehao、chengji、zuobiqingkuang(zuobileixing、zuobishijian、zuobitupian)、kaoshileixing、kaoshikemu、kaoshishijianduan、isAdmin、isExit、chuangjianshijian、gengxinshijian user = { "xuexiaomingcheng": "", "xuexiaodaihao": "", "zhuanyemingcheng": "", "zhuanyedaihao": "", "nianji": "", "banji": "", "xueshengxingming": "老师", "xuehao": "0000", "chengji": "", "zuobiqingkuang": [], "kaoshileixing": "", "kaoshikemu": "", "kaoshishijianduan": "", "isAdmin": 1, "isExit": 1, "chuangjianshijian": "", "gengxinshijian": "", } collection.insert_one(user) else: print(888, isAdmin) # 测试 @app.route("/") def hello(): return "Hello World!" # 新增用户 @app.route("/insert") def insert_data(): user = {"name": "John Doe", "age": 25, "city": "New York"} collection.insert_one(user) return "Data inserted successfully!" # 查询用户 @app.route("/query") def query_data(): users = collection.find() result = "" for user in users: result += f"Name: {user['name']}, Age: {user['age']}, City: {user['city']}
" return result # 更新用户 @app.route("/update") def update_data(): query = {"name": "John Doe"} new_data = {"$set": {"age": 30, "city": "San Francisco"}} collection.update_one(query, new_data) return "Data updated successfully!" # 删除用户 @app.route("/delete") def delete_data(): query = {"name": "John Doe"} collection.delete_one(query) return "Data deleted successfully!" if __name__ == "__main__": initData() app.run()