{ "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idapplication_iduser_iduser_typeform_contentsubmit_timeteam_iduser_dataorigin_form
0448995100280{\"姓名\": \"唐琪沂\", \"学号\": \"20250423\", \"是否经过监护人和学生本人共...2026-02-24 08:01:4419None{\"input82274\": \"唐琪沂\", \"input89185\": \"20250423\"...
144909584610{\"姓名\": \"瞿子渊\", \"学号\": \"20240533\", \"是否经过监护人和学生本人共...2026-02-24 08:01:4719None{\"input82274\": \"瞿子渊\", \"input89185\": \"20240533\"...
244919567380{\"姓名\": \"陈梓心\", \"学号\": \"26010065\", \"是否经过监护人和学生本人共...2026-02-24 08:01:5119None{\"input82274\": \"陈梓心\", \"input89185\": \"26010065\"...
344929576440{\"姓名\": \"刘斯馨\", \"学号\": \"20240402\", \"是否经过监护人和学生本人共...2026-02-24 18:03:2219None{\"input82274\": \"刘斯馨\", \"input89185\": \"20240402\"...
444939584650{\"姓名\": \"宋振豪\", \"学号\": \"20240576\", \"是否经过监护人和学生本人共...2026-02-24 08:02:4719None{\"input82274\": \"宋振豪\", \"input89185\": \"20240576\"...
..............................
79653199578560{\"姓名\": \"吴承轩\", \"学号\": \"20240705\", \"是否经过监护人和学生本人共...2026-02-25 13:24:2819None{\"input82274\": \"吴承轩\", \"input89185\": \"20240705\"...
79753209564430{\"姓名\": \"姚欣怡\", \"学号\": \"23010643\", \"是否经过监护人和学生本人共...2026-02-25 13:33:4819None{\"input82274\": \"姚欣怡\", \"input89185\": \"23010643\"...
798532195100630{\"姓名\": \"王韵佳\", \"学号\": \"20250479\", \"是否经过监护人和学生本人共...2026-02-25 13:44:2019None{\"input82274\": \"王韵佳\", \"input89185\": \"20250479\"...
79953229565930{\"姓名\": \"蔡玉汝\", \"学号\": \"23010007\", \"是否经过监护人和学生本人共...2026-02-25 13:46:5619None{\"input82274\": \"蔡玉汝\", \"input89185\": \"23010007\"...
80053239568140{\"姓名\": \"马滢然\", \"学号\": \"23010360\", \"是否经过监护人和学生本人共...2026-02-25 13:59:4419None{\"input82274\": \"马滢然\", \"input89185\": \"23010360\"...
\n", "

801 rows × 9 columns

\n", "
" ], "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 }