118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
# 第十周
|
||
# 例题
|
||
# SQLite连接方式
|
||
# import sqlite3
|
||
# conn = sqlite3.connect("数据库.db")
|
||
|
||
|
||
# mysql连接方式
|
||
# pip install pymysql --trusted-host mirrors.aliyun.com
|
||
# import pymysql
|
||
# conn = pymysql.connect(host='localhost',
|
||
# port=3306,
|
||
# user='username',
|
||
# password='password',
|
||
# db='database',
|
||
# charset='utf8')
|
||
|
||
# sql server连接方式
|
||
# pip install pymssql --trusted-host mirrors.aliyun.com
|
||
# import pymssql
|
||
# conn = pymssql.connect(server='localhost',
|
||
# user='username',
|
||
# password='password',
|
||
# database='database')
|
||
|
||
|
||
# import xlrd
|
||
# import pymysql
|
||
# def read_excel(file_name):
|
||
# wb = xlrd.open_workbook(file_name)
|
||
# sheet = wb.sheet_by_index(0)
|
||
# schools = []
|
||
# for row in range(1, sheet.nrows):
|
||
# school = []
|
||
# for col in range(sheet.ncols):
|
||
# school.append(sheet.cell_value(row,col))
|
||
# schools.append(school)
|
||
# return schools
|
||
|
||
|
||
# def insert_data(schools):
|
||
# conn = pymysql.connect(host='localhost',
|
||
# port=3306,
|
||
# user='root',
|
||
# password='123456',
|
||
# database='test',
|
||
# charset='utf8')
|
||
# cursor = conn.cursor()
|
||
# insert_sql = """
|
||
# insert into school(school_code, school_name, province, is_985, is_211, is_self_marking, school_type)
|
||
# values (%s, %s, %s, %s, %s, %s, %s)
|
||
# """
|
||
# for school in schools:
|
||
# cursor.execute(insert_sql, school)
|
||
# conn.commit()
|
||
# cursor.close()
|
||
# conn.close()
|
||
#
|
||
# school_datas = read_excel("school.xls")
|
||
# insert_data(school_datas)
|
||
|
||
|
||
|
||
|
||
|
||
# # 课后练习
|
||
# 已知某个班级的某次考试成绩信息包括学号、语文、数学、英语、总分,
|
||
# 该成绩信息保存在一张Excel表中,部分信息如图所示,现在要求将其读入到数据库中去。同时实现以下功能:
|
||
# # 创建数据表score, 读取excel中数据到数据库表中
|
||
# import xlrd
|
||
# import sqlite3
|
||
# def read_excels(file_name): #读取Excel文件
|
||
# wb = xlrd.open_workbook(file_name) #打开Excel文件,返回的是一个工作簿
|
||
# sheet = wb.sheet_by_index(0) #由工作簿得到表单
|
||
# students = [] #由行和列来获取文件中的信息,用列表students保存
|
||
# for row in range(1, sheet.nrows):
|
||
# student = []
|
||
# for col in range(sheet.ncols):
|
||
# student.append(transform(sheet.cell_value(row, col)))
|
||
# students.append(student)
|
||
# return students
|
||
# #
|
||
# def transform(data): # 返回的成绩是浮点数,尝试转换成整型
|
||
# try:
|
||
# return int(data)
|
||
# except: # 不能转换原样返回
|
||
# return data
|
||
# #
|
||
# def init_db(): # 新建数据库
|
||
# pass
|
||
|
||
# def insert(students): # 插入数据
|
||
# pass
|
||
|
||
# init_db() # 调用函数新建数据库
|
||
# students = read_excels("学生成绩表.xls") # 调用函数插入数据
|
||
# insert(students)
|
||
#
|
||
|
||
# 格式化输出
|
||
# def print_data(datas): # 打印结果
|
||
# print("{:^8}{:^8}{:^8}{:^8}{:^8}".format("num", "chinese", "math", "english", "total"))
|
||
# for data in datas:
|
||
# print("{:^8}{:^8}{:^8}{:^8}{:^8}".format(data[0], data[1], data[2], data[3], data[4]))
|
||
#
|
||
|
||
# # 1.按照总分排序
|
||
|
||
# # 2.获取所有存在不及格科目的学生记录
|
||
|
||
# # 3.获取指定科目的最高分、最低分以及平均分
|
||
|
||
# # 4.查询三科都及格的学生信息
|
||
|
||
# # 5.将所有学生的数学成绩都加5分
|
||
|
||
# # 6.删除语文不及格的学生记录,并打印删除了多少条记录
|