通过线程实现每秒保存
class Thread1(threading.Thread):
def __init__(self):# 初始化类的构造方法
super(Thread1,self).__init__()# 继承多线程的父类方法
def run(self): # 开启线程
while 1:
for i in StudentManager.s_list:
name = i.name
gender = i.gender
tel = i.tel
sql = 'update test set gender=%s,tel=%s where name=%s'
value = (gender, tel, name)
cursor.execute(sql, value)
conn.commit()
time.sleep(1)
t1 = Thread1() # 创建线程
t1.start() # 开启线程
连接数据库,通过数据库实现增删改查,以修改为例
# 2.4 修改学员信息
def modify_student(self):
# 1. 用户输入目标学员姓名
modify_name = input('请输入要修改的学员姓名:')
# 2. 遍历列表数据,如果学员存在修改姓名性别手机号,否则提示学员不存在
for i in self.student_list:
if modify_name == i.name:
i.name = input('姓名:')
i.gender = input('性别:')
i.tel = input('手机号:')
sql = " UPDATE test set gender=%s, tel=%s where name=%s"
value = (i.gender, i.tel, i.name)
try:
cursor.execute(sql, value)
conn.commit()
print('数据修改成功!')
except pymysql.Error as e:
print("数据修改失败:" + e)
conn.rollback()
print(f'修改学员信息成功,姓名{i.name}, 性别{i.gender}, 手机号{i.tel}')
break
else:
print('查无此人!')