Python数据库学生成绩管理系统
开发语言:Python
开发工具:pycharm
开发时间:2021年4月13号
一、 导入相关包
import sqlite3 #要先在pycharm里面下载好
import time
二、创建并打开数据库在数据库里创建一张学生表
def open_db():
con = sqlite3.connect("E:\\stu_db.db")
cur = con.execute('''create table if not exists score_table(userid integer primary key,username varchar(128),
yuwen_score integer,math_score integer,english_score integer,sum_score integer)''')
return cur, con
三、各功能方法
#输入学生信息方法
def info():
# userid = int(input("请输入学号:"))
username = input("请输入姓名:")
yuwen_score = int(input("请输入语文成绩:"))
math_score = int(input("请输入数学成绩:"))
english_score = int(input("请输入英语成绩:"))
sum_score = yuwen_score + math_score + english_score
return username, yuwen_score, math_score, english_score, sum_score
#是否继续此操作(用在其他功能方法中)
def is_continue():
yn_choose = input("是否继续,请按Y or N:")
if yn_choose == "y" or yn_choose == "Y":
return 1
elif yn_choose == "n" or yn_choose == "N":
return 0
#插入学生信息
def add_db():
# student = info()
userid = int(input("请输入学号:"))
a = open_db()
if is_id(userid, a) == 0:
student = info()
a[1].execute(
"insert into score_table(userid, username, yuwen_score, math_score, english_score, sum_score)\
values(?,?,?,?,?,?)", (userid, student[0], student[1], student[2], student[3], student[4]))
a[1].commit()
print("插入完成")
print("数据更新中.....")
time.sleep(2)
show_db()
a[1].close()
if is_continue() == 1:#调用是否继续功能函数
add_db()#用递归的思想
else:
print("此功能已退出,欢迎下次使用")
return
return
else:
print("插入失败,已有此学生学号")
if is_continue() == 1:
print("请重新输入!!")
add_db()
else:
print("此功能已退出,欢迎下次使用")
return
return
#判断学生学号是否存在
def is_id(ids, a):
# a = open_db()
flag = 0
cur = a[1].cursor()
cur.execute("select * from score_table")
a[1].commit()
for i in cur:
if ids == i[0]:
flag = 1
cur.close()
return 1
if flag == 0:
cur.close()
return 0
#删除学生信息方法
def del_db():
del_id = input("请输入想要删除的学号:")
del_ids = int(del_id)
a = open_db()
# cur = a[1].cursor()
if is_id(del_ids, a) == 1:
a[1].execute("delete from score_table where userid=" + del_id)
a[1].commit()
a[1].close()
print("恭喜你删除成功")
print("数据更新中......")
time.sleep(2)
print(" 删除之后的数据")
show_db()
return
else:
print("没有此学号,删除失败")
return
#展现学生表里的所有信息
def show_db():
print("----------------------------------------------------------------")
a = open_db()
cur = a[1].cursor()
cur.execute("select * from score_table")
print_p(cur)
return
#查询某个学生的信息
def search_db():
ids = input("请输入学号:")
id = int(ids)
a = open_db()
if is_id(id, a) == 1:
cur = a[1].cursor()
cur.execute("select * from score_table where userid=" + ids)
a[1].commit()
print("学号\t\t 姓名\t\t语文成绩\t\t数学成绩\t\t英语成绩\t\t 总分")
print("----------------------------------------------------------------")
for row in cur:
for h in row:
print(" ", end="")
print(h, end="\t\t ")
print("")
cur.close()
a[1].close()
if is_continue() == 1:
search_db()
else:
print("此功能已退出,欢迎下次使用")
return
return
else:
print("查找失败,没有此人")
if is_continue() == 1:
search_db()
else:
print("此功能已退出,欢迎下次使用")
return
return
#修改学生的信息
def alter_db():
id1 = input("请输入你想要修改学生的学号:")
ids = int(id1)
a = open_db()
if is_id(ids, a) == 1:
# cur = a[1].cursor()
student = info()
a[1].execute(
"update score_table set username=?, yuwen_score=?, math_score=?, english_score=?, sum_score=?\
where userid=" + id1, (student[0], student[1], student[2], student[3], student[4]))
a[1].commit()
print("修改成功")
print("数据更新中......")
time.sleep(2)
print(" 更新成功")
time.sleep(0.5)
show_db()
a[1].close()
if is_continue() == 1:
alter_db()
else:
print("此功能已退出,欢迎下次使用!!")
return
return
else:
print("修改失败,没有此学生")
if is_continue() == 1:
alter_db()
else:
print("此功能已退出,欢迎下次使用!!")
return
return
#对学生按照总成绩排序,然后打印出来
def sort_db():
a = open_db()
cur = a[1].cursor()
cur.execute("select * from score_table order by sum_score desc")
print_p(cur)
return
#打印每一行的学生信息
def print_p(cur):
res = cur.fetchall()
print("学号\t\t 姓名\t\t语文成绩\t\t数学成绩\t\t英语成绩\t\t 总分")
print("----------------------------------------------------------------")
for line in res:
for h in line:
print(" ", end="")
print(h, end="\t\t ")
print("")
cur.close()
return
四、菜单显示(主函数)
#菜单显示(主函数:类似与C语言的的main())
while True:
print("*****************************************")
print("** 增加学生信息——————————————————————1 **")
print("** 查找学生信息——————————————————————2 **")
print("** 删除学生信息----------------------3 **")
print("** 更改学生信息----------------------4 **")
print("** 输出学生信息----------------------5 **")
print("** 按总分成绩排名---------------------6 **")
print("*******************************************")
choose = input("请输入你的选择:")
if choose == "1":
add_db()
elif choose == "2":
search_db()
elif choose == "3":
del_db()
elif choose == "4":
alter_db()
elif choose == "5":
show_db()
elif choose == "6":
sort_db()