import pymysql
db = pymysql.connect(host="",
user="",
p"把这个删除,此英文是敏感词"assword="",
db="student",
charset="utf8",
port=3306,
cursorclass=pymysql.cursors.DictCursor
)
# 使用cursor()方法创建游标对象cursor
cursor = db.cursor()
def print_line():
"""
打印分隔符
:return:
"""
print("+" * 25)
def shouye():
"""
首页
:return:
"""
print_line()
print("\t欢迎来到学生管理系统!")
print("\t输入1进行学生录入!")
print("\t输入2进行学生查询!")
print("\t输入3进行学生统计!")
print("\t输入4进行学生删除!")
print("\t输入5进行成绩修改!")
print("\t输入6进行系统退出!")
print_line()
def luru():
"""
学生录入
:return:
"""
print_line()
A = "电信学院"
B = int(input("学号:"))
C = input("男还是女:")
D = input("请输入姓名:")
E = input("数学成绩:")
F = input("英语成绩:")
G = input("语文成绩:")
# 定义sql插入语句
sql_insert = "insert into students(xueyuan ,xuehao,sex,name,shuxue,yingyu,yuwen)values ('%s','%d','%s','%s','%s','%s','%s')" % (
A, B, C, D, E, F, G)
cursor.execute(sql_insert)
db.commit()
print_line()
print("录入成功!!!")
def chaxun():
"""
学生信息查询
:return:
"""
print_line()
sql_search = "select * from students"
cursor.execute(sql_search)
data = cursor.fetchall()
print("二级学院\t\t学号\t\t性别\t\t姓名\t\t\t语文\t\t\t数学\t\t英语")
for i in data:
print("%s\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\t%s" %(i["xueyuan"], i["xuehao"], i["sex"], i["name"],i["yuwen"], i["shuxue"], i["yingyu"]))
print_line()
print("如果你想查询单个学生的成绩可以输入他的姓名:")
a1=input("姓名是:")
sql_searchone = 'select * from students where name ="%s"'%(a1)
cursor.execute(sql_searchone)
one = cursor.fetchall()
for student in one:
if student["name"]==a1:
print("该学生的语文成绩为%s"%student["yuwen"])
print("该学生的英语成绩为%s" % student["yingyu"])
print("该学生的数学成绩为%s" % student["shuxue"])
else:
continue
print_line()
shouye()
def paixu():
"""
排序
:return:
"""
print("请输入你想排序的科目:")
a1 = input("科目是yuwen,yingyu,shxuue其中一个:")
print_line()
sql_search = 'select name from students order by "%s" asc'%(a1)
cursor.execute(sql_search)
data = cursor.fetchall()
for i in data:
print(i)
print("以上为按照改科目成绩降排序")
print_line()
def schu():
"""
删除
:return:
"""
print_line()
sql_search = "select * from students"
cursor.execute(sql_search)
data = cursor.fetchall()
print("二级学院\t\t学号\t\t性别\t\t姓名\t\t\t语文\t\t\t数学\t\t英语")
for i in data:
print("%s\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\t%s" % (
i["xueyuan"], i["xuehao"], i["sex"], i["name"], i["yuwen"], i["shuxue"], i["yingyu"]))
print_line()
a=input("请输入你想删除学生的名字:")
sql_search = 'delete from students where name="%s";' % (a)
cursor.execute(sql_search)
db.commit()
print("以下为新修改学生的数据")
print_line()
sql_search = "select * from students"
cursor.execute(sql_search)
data = cursor.fetchall()
print("二级学院\t\t学号\t\t性别\t\t姓名\t\t\t语文\t\t\t数学\t\t英语")
for i in data:
print("%s\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\t%s" % (
i["xueyuan"], i["xuehao"], i["sex"], i["name"], i["yuwen"], i["shuxue"], i["yingyu"]))
print_line()
def xiugai():
print_line()
sql_search = "select * from students"
cursor.execute(sql_search)
data = cursor.fetchall()
print("二级学院\t\t学号\t\t性别\t\t姓名\t\t\t语文\t\t\t数学\t\t英语")
for i in data:
print("%s\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\t%s" % (
i["xueyuan"], i["xuehao"], i["sex"], i["name"], i["yuwen"], i["shuxue"], i["yingyu"]))
a=int(input("请输入修改学生学号"))
b=input("请输入修改的科目")
c=input("请输入修改的分数")
sql_search = 'update students set %s="%s" where xuehao="%d";' % (b,c,a)
cursor.execute(sql_search)
db.commit()
print("以下为新修改学生的数据")
print_line()
sql_search = "select * from students"
cursor.execute(sql_search)
data = cursor.fetchall()
print("二级学院\t\t学号\t\t性别\t\t姓名\t\t\t语文\t\t\t数学\t\t英语")
for i in data:
print("%s\t\t%d\t\t%s\t\t%s\t\t%s\t\t%s\t%s" % (
i["xueyuan"], i["xuehao"], i["sex"], i["name"], i["yuwen"], i["shuxue"], i["yingyu"]))
print_line()
def quit1():
cursor.close()
db.close()
print("成功退出")
while (True):
jiezhi = int(input("请输入数字进行指令执行:"))
if (jiezhi == 1):
luru()
elif(jiezhi == 2):
chaxun()
elif (jiezhi == 3):
paixu()
elif (jiezhi == 4):
schu()
elif (jiezhi == 5):
xiugai()
elif (jiezhi ==6):
quit1()
break
else:
break
#没有写异常处理,sorry
pymysql.Connect()参数说明
host(str): MySQL服务器地址
port(int): MySQL服务器端口号
user(str): 用户名
passwd(str): 密码
db(str): 数据库名称
charset(str): 连接编码
请自行补充!mys