From 8ca6e16a86af1d7d5fb61077dd7e5adc2fde60e0 Mon Sep 17 00:00:00 2001 From: hck <2243793431@qq.com> Date: Tue, 28 May 2024 20:24:30 +0800 Subject: [PATCH] Commit message here --- db.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 db.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..b96cbb3 --- /dev/null +++ b/db.py @@ -0,0 +1,53 @@ +import json +class MysqlDatabase: + def __init__(self): + self.users = json.loads(open('users.json', mode='r',encoding='utf-8').read()) + self.students = json.loads(open('students.json', mode='r', encoding='utf-8').read()) + + + def check_login(self,username,password): + for user in self.users: + if username == user['username']: + if password == user['password']: + return True,'登陆成功' + else: + return False,'登录失败,密码错误' + return False,'登录失败,用户不存在' + + def all(self): + return self.students + + def insert(self,student): + self.students.append(student) + + def delete_by_username(self,name): + for student in self.students: + print(student) + if student['name'] == name: + self.students.remove(student) + return True,f'{name}删除学生成功' + return False,f'{name}学生不存在' + + def search_by_username(self,name): + for student in self.students: + if student['name'] == name: + return True, student + return False,f'{name}学生不存在' + + def update(self,stu): + for student in self.students: + if student['name'] == stu['name']: + student.update(stu) + return True,f'{stu["name"]} 学生数据修改成功' + return False,f'{stu["name"]} 学生不存在' + + + + +db = MysqlDatabase() + + + +if __name__ == '__main__': + db.check_login('admin','password') + print(db.all()) \ No newline at end of file