(一):程序介绍和流程图
(二):主要文件main.py
(三):基础文件basics.py
(四):管理学生信息manage.py
(五):查询学生信息query.py
(六):导入数据文件import_data.py
(七):导出数据文件export_data.py
重点: 这部分主要是用于查询,所以要多次用到查询语句,有很多个函数是为了使差不多类型的查询语句多次使用。
class Display():
“”“多个显示函数的集合”""
def main( cn):
“”“判断是否有学生存在,并根据选项调用函数query_all 和query_some “””
def query_all( cn):
“”“排序查询全部的学生,调用以下三个sort_by_函数”""
def sort_by_sno( cn):
“”“按学号排序查看”""
def sort_by_total( cn):
“”“按总分排序查看”""
def sort_by_single( cn):
“”“按单科成绩排序”""
class ShowReports():
“”“展示成绩表里的信息,多个显示函数的集合”""
按输入的参数返回"全部信息"的两个函数 :
def returnall_stu( cn, sel, sinfo):
“”“按学生表里的某个信息进行查找,返回全部信息”""
def returnall_repo( cn, sel, sinfo):
“”“按学生表里的某个信息进行查找,返回全部信息”""
def query_some( cn):
“”“查询特定的学生,调用以下不同参数的 common “””
def som_common( cn, sel, sinfo):
“”“query_some( cn) 里的选项可以通用的函数,可以根据 sel 的实参的不同而查询不同信息的学生”""
#如som_common( cn, ‘Sname’, sname)
def other_to_sno( cn, sel, sinfo):
“”“将Students表里的其他信息转换成 sno 返回”""
def show_students( information):
“”“展示学生表里的信息”""
import os
import basics as ba
class Display():
"""多个显示函数的集合"""
def __init__(self):
pass
def prefix( self, name):
print("****************************")
print(" %s菜单" % name)
print("****************************")
def main_show(self):
self.prefix("查询学生信息")
print("\t 1:查询特定的学生")
print("\t 2:排序查询")
print("\t 0:返回主菜单")
print("****************************\n")
def some_show(self):
self.prefix("查询特定的学生")
print("\t 1:按学号")
print("\t 2:按姓名")
print("\t 3:按性别")
print("\t 4:按班级")
print("\t 5:按居住地")
print("\t 0:返回查询菜单")
print("****************************\n")
def all_show(self):
self.prefix(" 排序查询")
print("\t 1:按学号排序查看")
print("\t 2:按总分排序查看")
print("\t 3:按单科成绩排序")
print("\t 0:返回查询菜单")
print("****************************\n")
# 按输入的参数返回"全部信息"的两个函数
def returnall_stu( cn, sel, sinfo):
"""按学生表里的某个信息进行查找,返回全部信息"""
sql = '''SELECT DISTINCT Sno, Sname, Sgender,
Sbirth, Sclass, Snative
FROM Students WHERE %s = '%s'
''' % ( sel, sinfo)
return common( cn, sql)
def returnall_repo( cn, sel, sinfo):
"""按学生表里的某个信息进行查找,返回全部信息"""
sql = '''SELECT DISTINCT Racademicyear,
Rterm, Sno, Cno, Grade
FROM Reports WHERE %s = '%s'
ORDER BY Racademicyear DESC, Rterm DESC
''' % (sel, sinfo)
return common( cn, sql)
#查询特定的学生
def show_students( information):
"""展示学生表里的信息"""
for row in information:
print("学号:",row[0])
print("姓名:",row[1])
print("性别:",row[2])
print("出生日期:",row[3])
print("班级:",row[4])
print("居住地:",row[5])
def common( cn, sql):
"""将数据从sql格式转换成列表镶嵌字典的格式并返回"""
cursor = cn.execute(sql)
information = []
for row in cursor:
information.append( row)
return information
def other_to_sno( cn, sel, sinfo):
"""将Students表里的其他信息转换成sno"""
sql = '''SELECT DISTINCT Sno
FROM Students
WHERE %s = '%s'
''' % ( sel, sinfo)
return common( cn, sql)
def som_common( cn, sel, sinfo):
"""query_some( cn)里的选项可以通用的函数"""
ssno = other_to_sno( cn, sel, sinfo)
if ssno is not None:
for stu in ssno:
for sno in stu:
i = input("\n按回车键显示下一个学生...")
print("\n**********************************\n")
onestu = returnall_stu( cn, 'Sno', sno)
show_students( onestu)
repo = returnall_repo( cn, 'Sno', sno)
if repo is not None:
print("\n学号%s的成绩为:" % sno)
sr = ShowReports( cn, repo)
sr.by_sno()
else:
print("没有成绩")
print("\n**********************************\n")
else:
print("不存在学生,请重新输入")
return
def query_some( cn):
"""查询特定的学生"""
display = Display()
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
display.some_show()
try:
m11 = int( input("请输入选项:"))
except ValueError:
print("请重新输入正确的数字选项")
continue
if m11 == 1: #按学号
sno = input("请输入需要查找的学生的学号:")
som_common( cn, 'Sno', sno)
elif m11 == 2: # 按姓名
sname = input("请输入需要查找的学生的姓名:")
som_common( cn, 'Sname', sname)
elif m11 == 3: # 按性别
sgender = input("请输入需要查找的学生的姓别:")
som_common( cn, 'Sgender', sgender)
elif m11 == 4: # 按班级
sclass = input("请输入需要查找的学生的班级:")
som_common( cn, 'Sclass', sclass)
elif m11 == 5: # 按居住地
snative = input("请输入需要查找的学生的居住地:")
som_common( cn, 'Snative', snative)
elif m11 == 0:
return
else:
print("无效的命令,请重新输入")
#排序查询学生
class ShowReports():
"""展示成绩表里的信息"""
def __init__(self, cn, information):
self.cn = cn
self.information = information
pass
def day(self, year, term):
print("选课学年:%-5s" % year, end = " ")
print("选课学期:%-5s" % term, end = " ")
return
def by_sno(self):
y = 0
for row in self.information:
if y != row[0]:
y = row[0]
print("\n%s年度:" % y)
cname = ba.cno_to_cname( self.cn, row[3])
self.day( row[0], row[1])
print("课程名:%-10s" % cname, end = " ")
print("成绩:%-5s" % row[4])
def by_total(self):
y = 0
for row in self.information:
if y != row[0]:
y = row[0]
print("\n%s年度:" % y)
self.day( row[0], row[1])
print("学号:%-11s" % row[2], end = " ")
print("总分:%-5s" % row[3])
def by_single(self):
y = 0
for row in self.information:
if y != row[0]:
y = row[0]
print("\n%s年度:" % y)
self.day( row[0], row[1])
print("学号:%-11s" % row[2], end = " ")
print("成绩:%-5s" % row[3])
def sort_by_sno( cn):
"""按学号排序查看"""
sql = '''SELECT DISTINCT Sno, Sname, Sgender,
Sbirth, Sclass, Snative
FROM Students
ORDER BY Sno
'''
stus = ba.common( cn, sql)
for onestu in stus:
i = input("\n按回车键显示下一个学生...")
print("\n**********************************\n")
print("学号:",onestu[0])
print("姓名:",onestu[1])
print("性别:",onestu[2])
print("出生日期:",onestu[3])
print("班级:",onestu[4])
print("居住地:",onestu[5])
repo = returnall_repo( cn, 'Sno', onestu[0])
if repo is not None:
print("\n学号%s的成绩为:" % onestu[0])
sr = ShowReports( cn, repo)
sr.by_sno()
else:
print("没有成绩")
print("\n**********************************\n")
def sort_by_total( cn):
"""按总分排序查看"""
sql = '''SELECT DISTINCT Racademicyear,
Rterm, Sno, SUM(Grade)
FROM Reports
GROUP BY Sno, Racademicyear, Rterm
ORDER BY Racademicyear DESC, Rterm DESC,
SUM(Grade) DESC
'''
stus = ba.common( cn, sql)
print("各学年各学期总分的排名为下:")
sr = ShowReports( cn, stus)
sr.by_total()
def sort_by_single( cn):
"""按单科成绩排序"""
sql = '''SELECT Cname
FROM Courses
'''
total_cname = ba.common( cn, sql)
while 1:
print("存在以下课程:")
i = 0
for row in total_cname:
print( row[0], end = " ")
i = i + 1
if i % 5 == 0:
print("")
print("")
cname = input("\n请输入课程名:")
count = ba.judge_comment\
( cn, 'Courses', 'Cno', 'Cname', cname)
if count == 0:
print("课程不存在,请重新输入")
continue
else:
cno = ba.cname_to_cno( cn, cname)
sql = '''SELECT DISTINCT Racademicyear,
Rterm, Sno,Grade
FROM Reports WHERE Cno = "%s"
ORDER BY Racademicyear DESC, Rterm DESC,
Grade DESC
''' % cno
stus = ba.common( cn, sql)
print("%s的排名为下:" % cname)
sr = ShowReports( cn, stus)
sr.by_single()
confirm = input("\n请问是否继续进行单科排序(Y/N)")
if confirm == 'n' or confirm == 'N':
return
def query_all( cn):
"""排序查询全部的学生"""
display = Display()
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
display.all_show()
try:
m12 = int( input("请输入选项:"))
except ValueError:
print("请重新输入正确的数字选项")
continue
if m12 == 1: #按学号排序查看
sort_by_sno( cn)
elif m12 == 2: #按总分排序查看
sort_by_total( cn)
elif m12 == 3: #按单科成绩排序
sort_by_single( cn)
elif m12 == 0:
return
else:
print("无效的命令,请重新输入")
#主函数
def main( cn):
"""判断是否有学生存在,并根据选项调用函数
query_all 和query_some """
sql = '''SELECT DISTINCT Sno, Sname, Sgender,
Sbirth, Sclass, Snative
FROM Students
'''
stus = ba.common( cn, sql)
count = 0
for row in stus:
count = count + 1
if count == 0:
print("数据库中没有学生,"
"请添加学生或导入数据")
i = input("\n按下回车键后,将清空屏幕,"
"返回主菜单")
i = os.system("cls")
return
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
display = Display()
display.main_show()
m1 = input("请输入选项:")
if m1 == '1': #查询特定的学生
query_some( cn)
elif m1 == '2': #排序查询全部的学生
query_all( cn)
elif m1 == '0':
i = input("\n按下回车键后,将清空屏幕,"
"返回主菜单")
i = os.system("cls")
return
else:
print("无效的命令,请重新输入")
如果文章对你有帮助,点赞是对我最好的鼓励了!