(一):程序介绍和流程图
(二):主要文件main.py
(三):基础文件basics.py
(四):管理学生信息manage.py
(五):查询学生信息query.py
(六):导入数据文件import_data.py
(七):导出数据文件export_data.py
导入全部信息,系统会遍历整个xls文件,左边的是文件中的表头,是文件自带的,不管文件中的是什么,只要我们找出数据库中和它对应的即可联系上,如左边是“学号”“学生的学号”,只要导入填的是“Sno”或“学号”就能成功。
课程表和选课表也是相同原理,全部导入完毕提示成功。
可以在导入全部信息后再分开导入科目成绩。
创建了一个语文成绩表,选取了导入全部信息中导入的三个已经存在的学生,加上三个新学生(当初新学生用了舍友的学号和名字,打了马赛克)
如果课程不存在,将会提示是否创建。
文件中一定要有学生的学号和姓名,不然新学生无法被创建,因为学生表的学号和姓名是一定不能为空的。如果只有学生的学号的话,那么得确保文件中的学生之前已经在数据库中。
def main( cn):
“”“调用import_all,import_one函数”""
def main_show():
“”“主函数的显示”""
def insert_students( cn, sno, sname):
“”“对学生表进行插入,区别与ba里的,这里不用自动插入Sclass”""
def import_all( cn):
“”“导入全部信息,调用all_的函数”""
#all的导入简单模仿了SQL Server的导入,需要自己输入导入到哪个表中,不然会出错
def all_students( cn, table):
“”“导入全部信息到 学生表 中”""
def all_courses( cn, table):
“”“导入全部信息到 课程表 中”""
def all_reports( cn, table):
“”“导入全部信息到 成绩表 中”""
def import_students( cn, table, header):
“”“新的文件信息导入到 学生表 “””
def import_courses( cn, table, header):
“”“新的文件信息导入到 课程表 “””
def import_reports( cn, table, header, nead):
“”“新的文件信息导入到 成绩表 “””
#nead是课程号,选课学年,选课学期是否需要输入
#如果nead中没有相应的key值,就需要从header中获取
#即不需要我们输入,在表格中本就存在
import os
import xlrd
import basics as ba
def insert_students( cn, sno, sname):
"""对学生表进行插入,区别与ba里的,这里不用自动插入Sclass"""
sql = '''insert into Students (Sno, Sname)
values('%s','%s')
''' % ( sno, sname)
cn.execute( sql)
cn.commit()
#从header获取相应的信息,导入到三个表的操作
def import_students( cn, table, header):
"""新的文件信息导入到学生表"""
stu = [ 'Sgender','Sbirth','Sclass','Snative']
nrows = table.nrows #表的行数
for i in range( 1, nrows):
onerow = table.row_values(i)
sno = onerow[ header['Sno']]
sno = str(int(sno))
sname = onerow[header['Sname']]
sql = "SELECT DISTINCT Sname FROM Students " \
"WHERE Sno = '%s'" % sno
cursor = cn.execute( sql)
information = 0
for row in cursor:
information = row[0]
if information != 0:
if information != onerow[header['Sname']]:
print("数据库中的已经存在这个学号,"
"对应的姓名为'%s',"
"而导入文件中的姓名为'%s'" %
(information, onerow[header['Sname']]))
confirm = input("信息冲突,是否覆盖?"
"(包括学生表和成绩表)(Y/N)")
if confirm == 'y' or confirm == 'Y':
ba.del_students( cn, sno)
insert_students( cn, sno, sname)
else:
print("取消覆盖,这行略过")
continue
else:
insert_students( cn, sno, sname)
for key, value in header.items():
if key in stu:
if key == 'Sbirth':
x = xlrd.xldate_as_tuple( onerow[ value], 0)
day = x[0:3]
onerow[ value] = str(day[0]) + '-' + str(day[1]) \
+ '-' + str(day[2])
elif key == 'Sclass':
onerow[ value] = str(int(onerow[ value]))
sql = '''UPDATE Students
SET %s = '%s'
WHERE Sno = '%s'
''' % ( key, onerow[ value], sno)
cn.execute( sql)
cn.commit()
def import_courses( cn, table, header):
"""新的文件信息导入到课程表"""
nrows = table.nrows #表的行数
cour = [ 'Chours', 'Ccredit']
for i in range( 1, nrows):
onerow = table.row_values(i)
cno = onerow[ header['Cno']]
cname = onerow[header['Cname']]
sql = "SELECT DISTINCT Cname FROM Courses " \
"WHERE Cno = '%s'" % cno
cursor = cn.execute( sql)
information = 0
for row in cursor:
information = row[0]
if information != 0:
if information != cname:
print("数据库中的已经存在这个课程号,"
"对应的课程名为'%s',"
"而导入文件中的课程名为'%s'" %
(information, cname))
confirm = input("信息冲突,是否覆盖"
"(包括课程表和成绩表)(Y/N)")
if confirm == 'y' or confirm == 'Y':
ba.del_courses( cn, cno)
ba.insert_courses2( cn, cno, cname)
else:
print("取消覆盖,这行略过")
continue
else:
ba.insert_courses2( cn, cno, cname)
for key, value in header.items():
if key in cour:
sql = '''UPDATE Courses
SET %s = '%s'
WHERE Cno = '%s'
''' % ( key, onerow[ value], cno)
cn.execute( sql)
cn.commit()
def import_reports( cn, table, header, nead):
"""新的文件信息导入到成绩表"""
#nead是课程号,选课学年,选课学期是否需要输入
#如果nead中没有相应的key值,就需要从header中获取
#即不需要我们输入,在表格中本就存在
nrows = table.nrows #表的行数
repo = ['Racademicyear', 'Rterm', 'Grade']
ccno = 0
cryear = 0
crterm = 0
for key,value in nead.items():
if key == 'Cno':
cno = value
ccno = 1
elif key == 'Racademicyear':
ryear = value
cryear = 1
elif key == 'Rterm':
rterm = value
crterm = 1
for i in range( 1, nrows):
onerow = table.row_values(i)
sno = onerow[ header['Sno']]
sno = str(int(sno))
if ccno == 0:
cno = onerow[header['Cno']]
if cryear == 0:
ryear = onerow[header['Racademicyear']]
if crterm == 0:
rterm = onerow[header['Rterm']]
sql = "SELECT DISTINCT Grade FROM Reports " \
"WHERE Sno = '%s' and Cno = '%s'" \
% ( sno, cno)
cursor = cn.execute( sql)
information = 0
for row in cursor:
information = row[0]
if information != 0:
print("数据库中的已经存在%s的%s"
"课程的信息"% ( sno, cno))
confirm = input("信息冲突,是否覆盖(Y/N)")
if confirm == 'n' or confirm == 'N':
print("取消覆盖,这行略过")
continue
else:
ba.insert_reports( cn, sno, cno, ryear, rterm)
for key, value in header.items():
if key in repo:
ba.update_reports\
( cn, sno, cno, key, onerow[ value])
# 导入全部信息
def all_students( cn, table):
"""导入全部信息到学生表中"""
data_header = table.row_values(0)
header = {}
for i, one_header in enumerate( data_header):
print( "文件中的:", one_header,end=" ")
want = input("导入:")
if want == '学号' or want == 'Sno':
header['Sno'] = i
elif want == '姓名' or want == 'Sname':
header['Sname'] = i
elif want == '性别' or want == 'Sgender':
header['Sgender'] = i
elif want == '出生日期' or want == 'Sbirth':
header['Sbirth'] = i
elif want == '班级' or want == 'Sclass':
header['Sclass'] = i
elif want == '居住地' or want == 'Snative':
header['Snative'] = i
import_students( cn, table, header)
def all_courses( cn, table):
"""导入全部信息到课程表中"""
data_header = table.row_values(0)
header = {}
for i, one_header in enumerate( data_header):
print( "文件中的:", one_header,end=" ")
want = input("导入:")
if want == '课程号' or want == 'Cno':
header['Cno'] = i
elif want == '课程名' or want == 'Cname':
header['Cname'] = i
elif want == '学时' or want == 'Chours':
header['Chours'] = i
elif want == '课程学分' or want == 'Ccredit':
header['Ccredit'] = i
import_courses( cn, table, header)
def all_reports( cn, table):
"""导入全部信息到成绩表中"""
data_header = table.row_values(0)
header = {}
for i, one_header in enumerate( data_header):
print( "文件中的:", one_header,end=" ")
want = input("导入:")
if want == '学号' or want == 'Sno':
header['Sno'] = i
elif want == '课程名' or want == 'Cno':
header['Cno'] = i
elif want == '选课学年' or want == 'Racademicyear':
header['Racademicyear'] = i
elif want == '选课学期' or want == 'Rterm':
header['Rterm'] = i
elif want == '成绩' or want == 'Grade':
header['Grade'] = i
nead = {}
import_reports( cn, table, header, nead)
def import_all( cn):
"""导入全部信息"""
file = ba.judge_file()
data = xlrd.open_workbook( file)
names = data.sheet_names()
for sheet_name in names:
print("\n这个表为:", sheet_name)
print("请问导入哪个表中:")
print("1:学生表\n2:课程表\n3:成绩表")
m = input( "请输入选项:")
table = data.sheet_by_name( sheet_name)
if m == '1':
all_students( cn, table)
elif m == '2':
all_courses( cn, table)
elif m == '3':
all_reports( cn, table)
print("导入%s中的信息成功" % file)
#导入单科成绩
def import_one( cn):
"""导入科目成绩"""
print("一个工作表只能存储一个科目的成绩,"
"否则导入会出错")
file = ba.judge_file()
data = xlrd.open_workbook( file)
names = data.sheet_names()
for sheet_name in names:
print("\n这个表为:", sheet_name)
cno = 0
while cno == 0:
cname = input("\n请输入要录入成绩的课程名:")
cno = ba.judge_creat_cname( cn, cname)
table = data.sheet_by_name( sheet_name)
data_header = table.row_values(0)
header = {}
for i, one_header in enumerate( data_header):
print( "文件中的:", one_header,end=" ")
want = input("导入:")
if want == '学号' or want == 'Sno':
header['Sno'] = i
elif want == '姓名' or want == 'Sname':
header['Sname'] = i
elif want == '班级' or want == 'Sclass':
header['Sclass'] = i
elif want == '选课学年' or want == 'Racademicyear':
header['Racademicyear'] = i
elif want == '选课学期' or want == 'Rterm':
header['Rterm'] = i
elif want == '成绩' or want == 'Grade':
header['Grade'] = i
nead = {}
nead['Cno'] = cno
count = 0
for key in header.keys():
if key == 'Racademicyear':
count = count + 1
elif key == 'Rterm':
count = count + 2
if count == 0:
print("表中没有相应的信息,", end = "")
nead['Racademicyear'] = input("请输入选课学年:")
nead['Rterm'] = input("请输入选课学期:")
elif count == 2:
print("表中没有相应的信息,", end = "")
nead['Racademicyear'] = input("请输入选课学年:")
import_students( cn, table, header)
import_reports( cn, table, header, nead)
print("导入%s中的信息成功" % file)
#主函数
def main_show():
"""主函数的显示"""
print("****************************")
print(" 导入菜单")
print("****************************")
print("\t 1:导入全部信息")
print("\t 2:导入科目成绩")
print("\t 0:返回主菜单")
print("****************************\n")
def main( cn):
"""调用import_all,import_one函数"""
while 1:
i = input("\n按下回车键后,将清空屏幕")
i = os.system("cls")
main_show()
m3 = input("请输入选项:")
if m3 == '1': #导入全部信息
import_all( cn)
elif m3 == '2': #导入科目成绩
import_one( cn)
elif m3 == '0':
i = input("\n按下回车键后,将清空屏幕,"
"返回主菜单")
i = os.system("cls")
return
else:
print("无效的命令,请重新输入")
如果文章对你有帮助,点赞是对我最好的鼓励了!