学生系统设计
主程序
from WinterSession.Python高级培训.seven.ManagerSystem import StudentManager
import threading
if __name__ == '__main__':
student_manager = StudentManager()
student_manager.run()
t1 = threading.Thread(target=student_manager.run)
t1.start()
t2= threading.Thread(target=student_manager.save_student)
t2.start()
学生类
class Student(object):
def __init__(self, name, gender, tel):
self.name = name
self.gender = gender
self.tel = tel
def __str__(self):
return f'{self.name}, {self.gender}, {self.tel}'
数据库操作
import pymysql
from ManagerSystem import StudentManager
ms = StudentManager()
def con():
db = pymysql.connect(host='localhost',
user='root',
password='baizhihao',
database='pymysql')
print('数据库连接成功')
return db
def insert1(name1,sex1,tel1):
conn = con()
cursor = conn.cursor()
sql = "insert into student1(name,sex, tel) values ('%s', '%s', '%s')" % (name1, sex1, tel1)
try:
cursor.execute(sql)
conn.commit()
print('数据插入成功')
except Exception as e:
conn.rollback()
print('插入数据失败')
print(e)
conn.close()
def delete1(name1):
conn = con()
cursor = conn.cursor()
sql = "delete from student1 where name = %s" % (name1)
try:
cursor.execute(sql)
conn.commit()
except Exception as e:
conn.rollback()
print('数据删除失败')
print(e)
conn.close()
def select1(name1):
conn = con()
cursor = conn.cursor()
sql = "select * from student1 where name = %s" % (name1)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
name = row[0]
sex = row[1]
tel = row[2]
print("姓名=%s,性别=%s,电话号码=%s" % \
(name, sex, tel))
except Exception as e:
print("数据查询失败")
print(e)
conn.close()
def update1(name1,sex1,tel1,name2):
conn = con()
cursor = conn.cursor()
sql = "update student1 set name = '%s' sex = '%s' tel = '%s' where name1 = '%s'" % (name1,sex1,tel1,name2)
try:
cursor.execute(sql)
conn.commit()
except Exception as e:
conn.rollback()
print(e)
print('数据修改失败')
conn.close()
def select2():
conn = con()
cursor = conn.cursor()
sql = "select * from student1 "
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
name = row[0]
sex = row[1]
tel = row[2]
print("姓名=%s,性别=%s,电话号码=%s" % \
(name, sex, tel))
except Exception as e:
print("数据查询失败")
print(e)
conn.close()
学生管理类
from student import *
import mysql1
import threading
import time
class StudentManager(object):
def __init__(self):
self.student_list = []
def run(self):
self.load_student()
while True:
self.show_menu()
menu_num = int(input('请输入您需要的功能序号:'))
if menu_num == 1:
self.add_student()
elif menu_num == 2:
self.del_student()
elif menu_num == 3:
self.modify_student()
elif menu_num == 4:
self.search_student()
elif menu_num == 5:
self.show_student()
elif menu_num == 6:
self.save_student()
elif menu_num == 7:
break
@staticmethod
def show_menu():
print('请选择如下功能:')
print('1:添加学员')
print('2:删除学员')
print('3:修改学员信息')
print('4:查询学员信息')
print('5:显示所有学员信息')
print('6:保存学员信息')
print('7:退出系统')
def add_student(self):
name = input('请输入您的姓名:')
gender = input('请输入您的性别:')
tel = input('请输入您的手机号:')
student = Student(name, gender, tel)
self.student_list.append(student)
mysql1.insert1(name,gender,tel)
print(self.student_list)
print(student)
def del_student(self):
del_name = input('请输入要删除的学员姓名:')
mysql1.delete1(del_name)
for i in self.student_list:
if del_name == i.name:
self.student_list.remove(i)
break
else:
print('查无此人!')
print(self.student_list)
def modify_student(self):
modify_name = input('请输入要修改的学员姓名:')
for i in self.student_list:
if modify_name == i.name:
i.name = input('姓名:')
i.gender = input('性别:')
i.tel = input('手机号:')
print(f'修改学员信息成功,姓名{i.name}, 性别{i.gender}, 手机号{i.tel}')
mysql1.update1(i.name, i.gender, i.tel, modify_name)
break
else:
print('查无此人!')
def search_student(self):
search_name = input('请输入您要搜索的学员姓名:')
for i in self.student_list:
if search_name == i.name:
print(f'姓名是{i.name}, 性别是{i.gender}, 手机号是{i.tel}')
mysql1.select1(search_name)
break
else:
print('查无此人!')
def show_student(self):
print('姓名\t性别\t手机号')
for i in self.student_list:
print(f'{i.name}\t{i.gender}\t{i.tel}')
mysql1.select2()
def save_student(self):
time.sleep(15)
f = open('student.data', 'w')
new_list = [i.__dict__ for i in self.student_list]
f.write(str(new_list))
f.close()
def load_student(self):
try:
f = open('student.data', 'r')
except:
f = open('student.data', 'w')
else:
data = f.read()
new_list = eval(data)
for i in new_list:
self.student_list = [Student(i['name'], i['gender'], i['tel'])]
finally:
f.close()
运行结果