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

547 lines
19 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 41,
"id": "66d26142",
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"import pandas as pd\n",
"import snowflake\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "7538343f",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 创建数据库连接\n",
"connection = pymysql.connect(\n",
"host='www.hxzhxy.cn',\n",
"user='root',\n",
"password='@HXYD1109mysql',\n",
"database='school_server',\n",
"charset='utf8'\n",
")\n",
"# 创建游标对象\n",
"cursor = connection.cursor()\n",
"sql=\"select id,stu_name from student where team_id=19\"\n",
"cursor.execute(sql)\n",
"result=cursor.fetchall()"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "8c88cbf4",
"metadata": {},
"outputs": [],
"source": [
"def create_table(cursor,result):\n",
" \n",
" data = []\n",
" for i in result:\n",
" list_list = list(i)\n",
" des=cursor.description # 获取表详情,字段名,长度,属性等\n",
" t = \",\".join([item[0] for item in des])\n",
" table_head = t.split(',') # # 查询表列名 用,分割\n",
" \n",
" dict_result = dict(zip(table_head, list_list)) # 打包为元组的列表 再转换为字典\n",
" data.append(dict_result) # 将字典添加到list_result中\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "11b4b922",
"metadata": {},
"outputs": [],
"source": [
"table = create_table(cursor,result)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "b0b8f2a4",
"metadata": {},
"outputs": [],
"source": [
"name2id = {item[\"stu_name\"]:item[\"id\"] for item in table}"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "c7b2c510",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4675"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(table)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "dcd74d67",
"metadata": {},
"outputs": [],
"source": [
"sql1 = \"select * from application_info where application_id=95\"\n",
"cursor.execute(sql1)\n",
"result=cursor.fetchall()\n",
"table_app = create_table(cursor,result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d305d2bd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 48,
"id": "8f20375f",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(table_app)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "93403945",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['id', 'application_id', 'user_id', 'user_type', 'form_content',\n",
" 'submit_time', 'team_id', 'user_data', 'origin_form'],\n",
" dtype='object')"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "88caddc9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>application_id</th>\n",
" <th>user_id</th>\n",
" <th>user_type</th>\n",
" <th>form_content</th>\n",
" <th>submit_time</th>\n",
" <th>team_id</th>\n",
" <th>user_data</th>\n",
" <th>origin_form</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>4489</td>\n",
" <td>95</td>\n",
" <td>10028</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"唐琪沂\", \"学号\": \"20250423\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-24 08:01:44</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"唐琪沂\", \"input89185\": \"20250423\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4490</td>\n",
" <td>95</td>\n",
" <td>8461</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"瞿子渊\", \"学号\": \"20240533\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-24 08:01:47</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"瞿子渊\", \"input89185\": \"20240533\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4491</td>\n",
" <td>95</td>\n",
" <td>6738</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"陈梓心\", \"学号\": \"26010065\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-24 08:01:51</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"陈梓心\", \"input89185\": \"26010065\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4492</td>\n",
" <td>95</td>\n",
" <td>7644</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"刘斯馨\", \"学号\": \"20240402\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-24 18:03:22</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"刘斯馨\", \"input89185\": \"20240402\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4493</td>\n",
" <td>95</td>\n",
" <td>8465</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"宋振豪\", \"学号\": \"20240576\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-24 08:02:47</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"宋振豪\", \"input89185\": \"20240576\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>796</th>\n",
" <td>5319</td>\n",
" <td>95</td>\n",
" <td>7856</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"吴承轩\", \"学号\": \"20240705\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-25 13:24:28</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"吴承轩\", \"input89185\": \"20240705\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>797</th>\n",
" <td>5320</td>\n",
" <td>95</td>\n",
" <td>6443</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"姚欣怡\", \"学号\": \"23010643\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-25 13:33:48</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"姚欣怡\", \"input89185\": \"23010643\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>798</th>\n",
" <td>5321</td>\n",
" <td>95</td>\n",
" <td>10063</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"王韵佳\", \"学号\": \"20250479\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-25 13:44:20</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"王韵佳\", \"input89185\": \"20250479\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>799</th>\n",
" <td>5322</td>\n",
" <td>95</td>\n",
" <td>6593</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"蔡玉汝\", \"学号\": \"23010007\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-25 13:46:56</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"蔡玉汝\", \"input89185\": \"23010007\"...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>800</th>\n",
" <td>5323</td>\n",
" <td>95</td>\n",
" <td>6814</td>\n",
" <td>0</td>\n",
" <td>{\"姓名\": \"马滢然\", \"学号\": \"23010360\", \"是否经过监护人和学生本人共...</td>\n",
" <td>2026-02-25 13:59:44</td>\n",
" <td>19</td>\n",
" <td>None</td>\n",
" <td>{\"input82274\": \"马滢然\", \"input89185\": \"23010360\"...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>801 rows × 9 columns</p>\n",
"</div>"
],
"text/plain": [
" id application_id user_id user_type \\\n",
"0 4489 95 10028 0 \n",
"1 4490 95 8461 0 \n",
"2 4491 95 6738 0 \n",
"3 4492 95 7644 0 \n",
"4 4493 95 8465 0 \n",
".. ... ... ... ... \n",
"796 5319 95 7856 0 \n",
"797 5320 95 6443 0 \n",
"798 5321 95 10063 0 \n",
"799 5322 95 6593 0 \n",
"800 5323 95 6814 0 \n",
"\n",
" form_content submit_time \\\n",
"0 {\"姓名\": \"唐琪沂\", \"学号\": \"20250423\", \"是否经过监护人和学生本人共... 2026-02-24 08:01:44 \n",
"1 {\"姓名\": \"瞿子渊\", \"学号\": \"20240533\", \"是否经过监护人和学生本人共... 2026-02-24 08:01:47 \n",
"2 {\"姓名\": \"陈梓心\", \"学号\": \"26010065\", \"是否经过监护人和学生本人共... 2026-02-24 08:01:51 \n",
"3 {\"姓名\": \"刘斯馨\", \"学号\": \"20240402\", \"是否经过监护人和学生本人共... 2026-02-24 18:03:22 \n",
"4 {\"姓名\": \"宋振豪\", \"学号\": \"20240576\", \"是否经过监护人和学生本人共... 2026-02-24 08:02:47 \n",
".. ... ... \n",
"796 {\"姓名\": \"吴承轩\", \"学号\": \"20240705\", \"是否经过监护人和学生本人共... 2026-02-25 13:24:28 \n",
"797 {\"姓名\": \"姚欣怡\", \"学号\": \"23010643\", \"是否经过监护人和学生本人共... 2026-02-25 13:33:48 \n",
"798 {\"姓名\": \"王韵佳\", \"学号\": \"20250479\", \"是否经过监护人和学生本人共... 2026-02-25 13:44:20 \n",
"799 {\"姓名\": \"蔡玉汝\", \"学号\": \"23010007\", \"是否经过监护人和学生本人共... 2026-02-25 13:46:56 \n",
"800 {\"姓名\": \"马滢然\", \"学号\": \"23010360\", \"是否经过监护人和学生本人共... 2026-02-25 13:59:44 \n",
"\n",
" team_id user_data origin_form \n",
"0 19 None {\"input82274\": \"唐琪沂\", \"input89185\": \"20250423\"... \n",
"1 19 None {\"input82274\": \"瞿子渊\", \"input89185\": \"20240533\"... \n",
"2 19 None {\"input82274\": \"陈梓心\", \"input89185\": \"26010065\"... \n",
"3 19 None {\"input82274\": \"刘斯馨\", \"input89185\": \"20240402\"... \n",
"4 19 None {\"input82274\": \"宋振豪\", \"input89185\": \"20240576\"... \n",
".. ... ... ... \n",
"796 19 None {\"input82274\": \"吴承轩\", \"input89185\": \"20240705\"... \n",
"797 19 None {\"input82274\": \"姚欣怡\", \"input89185\": \"23010643\"... \n",
"798 19 None {\"input82274\": \"王韵佳\", \"input89185\": \"20250479\"... \n",
"799 19 None {\"input82274\": \"蔡玉汝\", \"input89185\": \"23010007\"... \n",
"800 19 None {\"input82274\": \"马滢然\", \"input89185\": \"23010360\"... \n",
"\n",
"[801 rows x 9 columns]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "cf9f0877",
"metadata": {},
"outputs": [],
"source": [
"id1 = df[df.duplicated(subset=['user_id'],keep='last')]['id']\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "065978e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Series([], Name: id, dtype: int64)"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id1"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "3f2dce88",
"metadata": {},
"outputs": [],
"source": [
"for item in id1.tolist():\n",
" sql = f'delete from application_info where id = {item}'\n",
" cursor.execute(sql)\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "e325888a",
"metadata": {},
"outputs": [],
"source": [
"connection.commit()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "4c3c2181",
"metadata": {},
"outputs": [],
"source": [
"sql1 = \"select * from application_info where application_id=95\"\n",
"cursor.execute(sql1)\n",
"result=cursor.fetchall()\n",
"table_app = create_table(cursor,result)\n",
"\n",
" # cursor.execute(sql_update)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "3b6997e2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"799"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(table_app)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "e65e7d7f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'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}'}\n",
"{'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}'}\n",
"{'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}'}\n",
"{'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}'}\n",
"{'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}'}\n"
]
}
],
"source": [
"import json\n",
"for item in table_app:\n",
" tem = json.loads(item[\"form_content\"])\n",
" name = tem['姓名']\n",
" stuid = name2id.get(name,None)\n",
" if not stuid:\n",
" print(item)\n",
" continue\n",
" # else:\n",
" # sql_update = f'''update application_info set user_id={stuid} where id={item[\"id\"]}'''\n",
" # cursor.execute(sql_update)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "0e9ba4bd",
"metadata": {},
"outputs": [],
"source": [
"connection.commit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5ba39d1",
"metadata": {},
"outputs": [],
"source": [
"黄俊英"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}