import pymysql
import threading
import time
class StudentManage():
def Bulid(self):
conn = pymysql.connect(host='localhost', user='root', password='qq123123', database='test')
print("连接成功")
corsor = conn.cursor()
corsor.execute('DROP TABLE IF EXISTS Student')
sqlQuery = "CREATE TABLE Student(Name CHAR(20) NOT NULL ,Gender CHAR(20) NOT NULL ,NUM CHAR(15) NOT NULL)"
corsor.execute(sqlQuery)
def Manage(self):
conn = pymysql.connect(host='localhost', user='root', password='qq123123', database='test')
corsor = conn.cursor()
num = 1
while(num != 5):
num = input("输入你要进行的功能:")
print(num)
num = int(num)
if num == 1:
name = input('请输入学生姓名:')
gender = input('请输入学生的性别:')
number = input('请输入学生的学号:')
corsor = conn.cursor()
sqlQuery = " INSERT INTO Student(Name,Gender,NUM) VALUE (%s,%s,%s)"
value = (name, gender, number)
corsor.execute(sqlQuery, value)
conn.commit()
print("完成")
elif num == 2:
stu = input('请输入你要修改的学生姓名:')
sqlQuery1 = 'select * from Student where Name = %s'
args = (stu,)
corsor.execute(sqlQuery1, args)
results = corsor.fetchall()
results = list(results)
if len(results) != 0:
m_info = input('请输入你要修改的信息:')
d_info = input('请输入你要将它修改的内容:')
if m_info == 'Gender':
sqlQuery = 'UPDATE Student SET Gender=%s WHERE Name = %s'
args = (d_info, stu)
corsor.execute(sqlQuery, args)
conn.commit()
print("完成")
elif m_info == 'Name':
sqlQuery = 'UPDATE Student SET Name=%s WHERE Name = %s'
args = (d_info, stu)
corsor.execute(sqlQuery, args)
conn.commit()
print("完成")
elif m_info == 'NUM':
sqlQuery = 'UPDATE Student SET NUM=%s WHERE Name = %s'
args = (d_info, stu)
corsor.execute(sqlQuery, args)
conn.commit()
print("完成")
else:
print("该生不存在")
elif num == 3:
del_stu = input("请输入你要删除的学生姓名:")
sqlQuery1 = 'select * from Student where Name = %s'
args = (del_stu,)
corsor.execute(sqlQuery1, args)
results = corsor.fetchall()
results = list(results)
if len(results) != 0:
sqlQuery = 'delete from Student where Name=%s'
args = (del_stu,)
corsor.execute(sqlQuery, args)
conn.commit()
print("完成")
else:
print("该生不存在")
elif num == 4:
cha_stu = input("请输入你要查询的学生信息:")
sqlQuery = 'select * from Student where Name = %s'
args = (cha_stu,)
corsor.execute(sqlQuery, args)
results = corsor.fetchall()
results1 = list(results)
if len(results1) != 0:
print(results)
else:
print("该生不存在")
conn.commit()
print("感谢使用")
def show():
print("----------------欢迎使用学生管理系统------------------")
print("请选择如下功能:")
print("1:添加学生的信息")
print("2:修改学生的信息")
print("3:删除学生的信息")
print("4:查询学生的信息")
print("5:退出学生系统")
if __name__ == '__main__':
show()
student = StudentManage()
student.Manage()
|