Perform related operations on MySQL database in PyCharm

According to the student performance management database created in Experiment 2.1, as well as the student table, curriculum table and student performance table, use Python code and SQL statements to complete the data operation function in Python. Complete the experiment as required, and give the code and screenshots of important steps:

(1) Connect to the database qiangzi in the Python code.

(2) Use the Python code to insert the following two pieces of data into the student score sheet.

M001

0531

2019-2-26 13:15:12

77

0.4

K002

0591

2018-2-26 13:15:12

82.5

0.4

86

(3) Use Python code to modify the grades of students whose usual grades and final grades are empty to 0.

(4) Use Python code to query the total score of each course of all students (total score = usual score * usual score weight + test score * (1 - usual score weight)) and output.

(5) Use Python code to count the number of students whose final grades are lower than the total average score of all students.

(6) Use Python code to delete the data whose usual grades and final grades are 0, and output the number of deleted records.

# @Time: 2023/5/4 22:22
# @Author: 马龙强
# @File: Mysql.py
# @software: PyCharm
import pymysql
# 打开数据库连接
db = pymysql.connect(host='localhost',
                     user='root',
                     password='789456111',
                     port=3306,
                     db='qiangzi')
print('连接成功');
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 插入数据
sql_insert = """INSERT INTO record (cn, sn, cst, up, tatio, exam)
                VALUES ('M001', '0531', '2019-2-26 13:15:12', 77, 0.4, null),
                       ('K002', '0591', '2018-2-26 13:15:12', 82.5, 0.4, 86)"""
cursor.execute(sql_insert)
db.commit()

# 修改成绩为0的学生信息
sql_update = """UPDATE record SET up=0, exam=0 WHERE up IS NULL AND exam IS NULL"""
cursor.execute(sql_update)
db.commit()



# 查询每门课程的总成绩并输出
sql_total_score = """SELECT cn, sn, up * tatio + exam * (1 - tatio) AS total_score
                     FROM record"""
cursor.execute(sql_total_score)
results = cursor.fetchall()
for row in results:
    print(f"学生{row[1]}的{row[0]}总成绩为:{row[2]}")

# 统计期末成绩低于平均分的学生人数
sql_avg_score = """SELECT AVG(up * tatio + exam * (1 - tatio)) AS avg_score
                   FROM record"""
cursor.execute(sql_avg_score)
result = cursor.fetchone()
avg_score = result[0]
sql_less_than_avg = f"""SELECT COUNT(*) FROM record
                        WHERE (up * tatio + exam * (1 - tatio)) < {avg_score}"""
cursor.execute(sql_less_than_avg)
result = cursor.fetchone()
print(f"期末成绩低于平均分的学生人数有:{result[0]}")

# 删除平时成绩和期末成绩都为0的数据,并输出删除的记录数
sql_delete = """DELETE FROM record WHERE up=0 AND exam=0"""
cursor.execute(sql_delete)
db.commit()
deleted_num = cursor.rowcount
print(f"已删除{deleted_num}条数据")

# 关闭数据库连接
db.close()

Note: cn, sn, cst, up, tatio, and exam in the code correspond to the course number, student number, course selection time, usual grades, weighting of usual grades, and test scores in the student record.

Guess you like

Origin blog.csdn.net/m0_74972727/article/details/130507868