Files
python----/数据库/线上/恢复学生数据.ipynb
T
admin aac9f5934d feat: 添加多个功能模块和工具脚本
- 新增websocket客户端和服务端实现
- 添加图片压缩工具和快速压缩脚本
- 实现学生信息处理相关API
- 添加MQTT客户端和消息处理功能
- 更新.gitignore忽略更多文件类型
- 添加数据库操作工具和示例
- 实现多个测试脚本和工具类
2026-04-13 14:47:50 +08:00

19 KiB
Raw Blame History

In [41]:
import pymysql
import pandas as pd
import snowflake
import random
In [42]:

# 创建数据库连接
connection = pymysql.connect(
host='www.hxzhxy.cn',
user='root',
password='@HXYD1109mysql',
database='school_server',
charset='utf8'
)
# 创建游标对象
cursor = connection.cursor()
sql="select id,stu_name from student where team_id=19"
cursor.execute(sql)
result=cursor.fetchall()
In [43]:
def create_table(cursor,result):
    
    data = []
    for i in result:
        list_list = list(i)
        des=cursor.description  # 获取表详情,字段名,长度,属性等
        t = ",".join([item[0] for item in des])
        table_head = t.split(',')  # # 查询表列名 用,分割
    
        dict_result = dict(zip(table_head, list_list))  # 打包为元组的列表 再转换为字典
        data.append(dict_result)  # 将字典添加到list_result中
    return data
In [44]:
table = create_table(cursor,result)
In [45]:
name2id = {item["stu_name"]:item["id"] for item in table}
In [46]:
len(table)
Out [46]:
4675
In [47]:
sql1 = "select * from application_info where application_id=95"
cursor.execute(sql1)
result=cursor.fetchall()
table_app = create_table(cursor,result)
In [ ]:
In [48]:
df = pd.DataFrame(table_app)
In [49]:
df.columns
Out [49]:
Index(['id', 'application_id', 'user_id', 'user_type', 'form_content',
       'submit_time', 'team_id', 'user_data', 'origin_form'],
      dtype='object')
In [50]:
df
Out [50]:
id application_id user_id user_type form_content submit_time team_id user_data origin_form
0 4489 95 10028 0 {"姓名": "唐琪沂", "学号": "20250423", "是否经过监护人和学生本人共... 2026-02-24 08:01:44 19 None {"input82274": "唐琪沂", "input89185": "20250423"...
1 4490 95 8461 0 {"姓名": "瞿子渊", "学号": "20240533", "是否经过监护人和学生本人共... 2026-02-24 08:01:47 19 None {"input82274": "瞿子渊", "input89185": "20240533"...
2 4491 95 6738 0 {"姓名": "陈梓心", "学号": "26010065", "是否经过监护人和学生本人共... 2026-02-24 08:01:51 19 None {"input82274": "陈梓心", "input89185": "26010065"...
3 4492 95 7644 0 {"姓名": "刘斯馨", "学号": "20240402", "是否经过监护人和学生本人共... 2026-02-24 18:03:22 19 None {"input82274": "刘斯馨", "input89185": "20240402"...
4 4493 95 8465 0 {"姓名": "宋振豪", "学号": "20240576", "是否经过监护人和学生本人共... 2026-02-24 08:02:47 19 None {"input82274": "宋振豪", "input89185": "20240576"...
... ... ... ... ... ... ... ... ... ...
796 5319 95 7856 0 {"姓名": "吴承轩", "学号": "20240705", "是否经过监护人和学生本人共... 2026-02-25 13:24:28 19 None {"input82274": "吴承轩", "input89185": "20240705"...
797 5320 95 6443 0 {"姓名": "姚欣怡", "学号": "23010643", "是否经过监护人和学生本人共... 2026-02-25 13:33:48 19 None {"input82274": "姚欣怡", "input89185": "23010643"...
798 5321 95 10063 0 {"姓名": "王韵佳", "学号": "20250479", "是否经过监护人和学生本人共... 2026-02-25 13:44:20 19 None {"input82274": "王韵佳", "input89185": "20250479"...
799 5322 95 6593 0 {"姓名": "蔡玉汝", "学号": "23010007", "是否经过监护人和学生本人共... 2026-02-25 13:46:56 19 None {"input82274": "蔡玉汝", "input89185": "23010007"...
800 5323 95 6814 0 {"姓名": "马滢然", "学号": "23010360", "是否经过监护人和学生本人共... 2026-02-25 13:59:44 19 None {"input82274": "马滢然", "input89185": "23010360"...

801 rows × 9 columns

In [51]:
id1 = df[df.duplicated(subset=['user_id'],keep='last')]['id']
In [52]:
id1
Out [52]:
Series([], Name: id, dtype: int64)
In [33]:
for item in id1.tolist():
    sql = f'delete from application_info where id = {item}'
    cursor.execute(sql)
In [34]:
connection.commit()
In [17]:
sql1 = "select * from application_info where application_id=95"
cursor.execute(sql1)
result=cursor.fetchall()
table_app = create_table(cursor,result)

        # cursor.execute(sql_update)
In [18]:
len(table_app)
Out [18]:
799
In [22]:
import json
for item in table_app:
    tem = json.loads(item["form_content"])
    name = tem['姓名']
    stuid = name2id.get(name,None)
    if not stuid:
        print(item)
        continue
    # else:
    #     sql_update = f'''update application_info set user_id={stuid} where id={item["id"]}'''
    #     cursor.execute(sql_update)
{'id': 4602, 'application_id': 95, 'user_id': 7000, 'user_type': 0, 'form_content': '{"姓名": "导师一班刘梓豪", "学号": "20250321", "是否经过监护人和学生本人共同慎重考虑,自愿申报晚自习": "是"}', 'submit_time': datetime.datetime(2026, 2, 24, 8, 45, 58), 'team_id': 19, 'user_data': None, 'origin_form': '{"input82274": "导师一班刘梓豪", "input89185": "20250321", "radio23109": 1}'}
{'id': 4740, 'application_id': 95, 'user_id': 3031, 'user_type': 0, 'form_content': '{"姓名": "黄俊英", "学号": "23010155", "是否经过监护人和学生本人共同慎重考虑,自愿申报晚自习": "是"}', 'submit_time': datetime.datetime(2026, 2, 24, 9, 39, 14), 'team_id': 19, 'user_data': None, 'origin_form': '{"input82274": "黄俊英", "input89185": "23010155", "radio23109": 1}'}
{'id': 4757, 'application_id': 95, 'user_id': 4138, 'user_type': 0, 'form_content': '{"姓名": "李沛霖", "学号": "20240313", "是否经过监护人和学生本人共同慎重考虑,自愿申报晚自习": "是"}', 'submit_time': datetime.datetime(2026, 2, 24, 9, 48, 29), 'team_id': 19, 'user_data': None, 'origin_form': '{"input82274": "李沛霖", "input89185": "20240313", "radio23109": 1}'}
{'id': 4758, 'application_id': 95, 'user_id': 3620, 'user_type': 0, 'form_content': '{"姓名": "23010047", "学号": "陈希", "是否经过监护人和学生本人共同慎重考虑,自愿申报晚自习": "是"}', 'submit_time': datetime.datetime(2026, 2, 24, 9, 48, 20), 'team_id': 19, 'user_data': None, 'origin_form': '{"input82274": "23010047", "input89185": "陈希", "radio23109": 1}'}
{'id': 5006, 'application_id': 95, 'user_id': 4269, 'user_type': 0, 'form_content': '{"姓名": "杨凤", "学号": "20240575", "是否经过监护人和学生本人共同慎重考虑,自愿申报晚自习": "是"}', 'submit_time': datetime.datetime(2026, 2, 24, 13, 41, 33), 'team_id': 19, 'user_data': None, 'origin_form': '{"input82274": "杨凤", "input89185": "20240575", "radio23109": 1}'}
In [21]:
connection.commit()
In [ ]:
黄俊英