parent
5f489e35c3
commit
098574a59f
@ -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())
|
Loading…
Reference in new issue