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

25 KiB

In [ ]:
import pymysql
import snowflake
from faker import Faker
fake = Faker('zh_CN')
import random
# 创建数据库连接
connection = pymysql.connect(
host='www.hxzhxy.cn',
user='root',
password='@HXYD1109mysql',
database='school_server',
charset='utf8'
)
# 创建游标对象
cursor = connection.cursor()
sql="select * from robot_knowledge"
cursor.execute(sql)
result=cursor.fetchall()
In [ ]:
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 [ ]:
table = create_table(cursor,result)
In [ ]:
import pandas as pd
In [ ]:
df = pd.DataFrame(table)
In [ ]:
df.value_counts('team_id')
In [ ]:

'''INSERT INTO `school_server`.`robot_knowledge_role` ( `role_id`, `knowledge_id`, `team_id`, `create_time`, `update_time`, `doc_id`, `dataset_id`) VALUES (47, 100, 84, 106, '2025-11-03 16:52:53', '2025-11-03 16:52:53', 7568416160399245339, NULL);
'''
In [ ]:
table[1]
In [27]:
values = [f'''(200, {item["id"]}, {item['team_id']}, {item['doc_id']}, {item['dataset_id']})''' for item in table if item['team_id']==106]
In [28]:
values = ','.join(values)
In [29]:
values
Out [29]:
'(200, 1, 106, 7493798895450538011, 7484070579806076980),(200, 2, 106, 7493793640993783820, 7484070579806076980),(200, 3, 106, 7487788096340541490, 7484070579806076980),(200, 4, 106, 7493831983656599588, 7484070579806076980),(200, 5, 106, 7493829638301581346, 7484070579806076980),(200, 6, 106, 7493833309840638002, 7484070579806076980),(200, 7, 106, 7493831831671881740, 7484070579806076980),(200, 8, 106, 7504958246848069695, 7484070579806076980),(200, 9, 106, 7493831043427254287, 7484070579806076980),(200, 10, 106, 7493828500946026523, 7484070579806076980),(200, 12, 106, 7493830959789097010, 7484070579806076980),(200, 14, 106, 7493828993298759680, 7484070579806076980),(200, 15, 106, 7493828981784281140, 7484070579806076980),(200, 16, 106, 7493828981575532559, 7484070579806076980),(200, 17, 106, 7493826379621793801, 7484070579806076980),(200, 18, 106, 7493829827553738786, 7484070579806076980),(200, 19, 106, 7493827900854468623, 7484070579806076980),(200, 64, 106, 7504123255642325000, 7484070579806077000),(200, 170, 106, 7504929805932019712, 7484070579806076980),(200, 171, 106, 7504930636316295168, 7484070579806076980),(200, 173, 106, 7504929616852451391, 7484070579806076980),(200, 174, 106, 7504929523592151066, 7484070579806076980),(200, 175, 106, 7504929527064281097, 7484070579806076980),(200, 176, 106, 7504929527064330249, 7484070579806076980),(200, 177, 106, 7504932503968333862, 7484070579806076980),(200, 179, 106, 7504933626712440832, 7484070579806076980),(200, 180, 106, 7504934665473409035, 7484070579806076980),(200, 181, 106, 7504934665683189801, 7484070579806076980),(200, 182, 106, 7504941148646817844, 7484070579806076980),(200, 183, 106, 7504940351712313359, 7484070579806076980),(200, 188, 106, 7504941839004090420, 7484070579806076980),(200, 192, 106, 7504947533727580186, 7484070579806076980),(200, 193, 106, 7504948407497916431, 7484070579806076980),(200, 208, 106, 7504960381623058441, 7484070579806076980),(200, 217, 106, 7504961143317561384, 7484070579806076980),(200, 220, 106, 7504965659202748451, 7484070579806076980),(200, 227, 106, 7504967980234129442, 7484070579806076980),(200, 228, 106, 7504969293270122511, 7484070579806076980),(200, 229, 106, 7504968994516893748, 7484070579806076980),(200, 230, 106, 7504969659939864585, 7484070579806076980),(200, 231, 106, 7504970197435220018, 7484070579806076980),(200, 232, 106, 7504969659939913737, 7484070579806076980),(200, 233, 106, 7504969843867697171, 7484070579806076980),(200, 234, 106, 7504969893033623567, 7484070579806076980),(200, 235, 106, 7504969659939979273, 7484070579806076980),(200, 236, 106, 7504968426901897216, 7484070579806076980),(200, 237, 106, 7504970197641560090, 7484070579806076980),(200, 238, 106, 7504969844489076748, 7484070579806076980),(200, 239, 106, 7504969873518788646, 7484070579806076980),(200, 240, 106, 7504970367069437963, 7484070579806076980),(200, 241, 106, 7504968426902093824, 7484070579806076980),(200, 242, 106, 7504968426902110208, 7484070579806076980),(200, 243, 106, 7504969844489125900, 7484070579806076980),(200, 244, 106, 7504970197641674778, 7484070579806076980),(200, 245, 106, 7504969893034180623, 7484070579806076980),(200, 246, 106, 7504969907315572776, 7484070579806076980),(200, 247, 106, 7504968426902290432, 7484070579806076980),(200, 248, 106, 7504971290529251354, 7484070579806076980),(200, 249, 106, 7504968994517516340, 7484070579806076980),(200, 250, 106, 7504972149074559010, 7484070579806076980),(200, 251, 106, 7504970954980769819, 7484070579806076980),(200, 252, 106, 7504972243660079138, 7484070579806076980),(200, 253, 106, 7504972243660095522, 7484070579806076980),(200, 254, 106, 7504971459249750028, 7484070579806076980),(200, 255, 106, 7504970300418490409, 7484070579806076980),(200, 256, 106, 7504972579393306665, 7484070579806076980),(200, 257, 106, 7504972243660701730, 7484070579806076980),(200, 258, 106, 7504972243660734498, 7484070579806076980),(200, 259, 106, 7504973775029485577, 7484070579806076980),(200, 260, 106, 7504973222212812837, 7484070579806076980),(200, 261, 106, 7504969293744029696, 7484070579806076980),(200, 262, 106, 7504973436738158633, 7484070579806076980),(200, 263, 106, 7504974642226954276, 7484070579806076980),(200, 264, 106, 7504973775029747721, 7484070579806076980),(200, 265, 106, 7504973775029780489, 7484070579806076980),(200, 266, 106, 7504972244134395938, 7484070579806076980),(200, 267, 106, 7504972581586894875, 7484070579806076980),(200, 268, 106, 7504972581586911259, 7484070579806076980),(200, 269, 106, 7504975438927724584, 7484070579806076980),(200, 270, 106, 7504975438927740968, 7484070579806076980),(200, 271, 106, 7504976476929556530, 7484070579806076980),(200, 272, 106, 7504975868206350347, 7484070579806076980),(200, 273, 106, 7504979646199480330, 7484070579806076980),(200, 274, 106, 7504975438927773736, 7484070579806076980),(200, 275, 106, 7504972685018972212, 7484070579806076980),(200, 276, 106, 7504974960579035151, 7484070579806076980),(200, 277, 106, 7504972685018988596, 7484070579806076980),(200, 278, 106, 7504972685019021364, 7484070579806076980),(200, 279, 106, 7504972685019037748, 7484070579806076980),(200, 280, 106, 7504975438927855656, 7484070579806076980),(200, 281, 106, 7504976003066200074, 7484070579806076980),(200, 282, 106, 7504974960579051535, 7484070579806076980),(200, 283, 106, 7504975438927872040, 7484070579806076980),(200, 284, 106, 7504975438927888424, 7484070579806076980),(200, 285, 106, 7504974960579248143, 7484070579806076980),(200, 286, 106, 7504972685019070516, 7484070579806076980),(200, 287, 106, 7504975438927904808, 7484070579806076980),(200, 288, 106, 7504972685019086900, 7484070579806076980),(200, 289, 106, 7504979646199644170, 7484070579806076980),(200, 290, 106, 7504972685019103284, 7484070579806076980),(200, 291, 106, 7504979008561020991, 7484070579806076980),(200, 292, 106, 7504975906710503465, 7484070579806076980),(200, 293, 106, 7504978010504904740, 7484070579806076980),(200, 294, 106, 7504979008561053759, 7484070579806076980),(200, 295, 106, 7504976476929769522, 7484070579806076980),(200, 296, 106, 7504972685019136052, 7484070579806076980),(200, 297, 106, 7504972685019152436, 7484070579806076980),(200, 298, 106, 7504972685019168820, 7484070579806076980),(200, 299, 106, 7504976003066265610, 7484070579806076980),(200, 300, 106, 7504975438928019496, 7484070579806076980),(200, 301, 106, 7504976476929785906, 7484070579806076980),(200, 302, 106, 7504975438928035880, 7484070579806076980),(200, 303, 106, 7504972685019201588, 7484070579806076980),(200, 304, 106, 7504972685019217972, 7484070579806076980),(200, 305, 106, 7504974960579330063, 7484070579806076980),(200, 306, 106, 7504978181490049039, 7484070579806076980),(200, 307, 106, 7504974960579362831, 7484070579806076980),(200, 308, 106, 7504976476929933362, 7484070579806076980),(200, 309, 106, 7504979008561135679, 7484070579806076980),(200, 310, 106, 7504979008561152063, 7484070579806076980),(200, 311, 106, 7504975439128903714, 7484070579806076980),(200, 329, 106, 7506421855394955327, 7484070579806076980),(200, 336, 106, 7514572124083650594, 7484070579806076980),(200, 337, 106, 7515018357037957172, 7484070579806076980),(200, 338, 106, 7521946826728423462, 7484070579806076980),(200, 339, 106, 7522316877478953012, 7484070579806076980),(200, 340, 106, 7522673039637561382, 7484070579806076980),(200, 341, 106, 7532759381411610624, 7484070579806076980),(200, 342, 106, 7535307477702934571, 7484070579806076980),(200, 343, 106, 7535306311941701641, 7484070579806076980),(200, 344, 106, 7535366138773078050, 7484070579806076980)'
In [ ]:
for item in table:
    values = [f'''(100, {item["id"]}, {item['team_id']}, {item['doc_id']}, {item['dataset_id']})''' for item in table]
    sql = f'''INSERT INTO `school_server`.`robot_knowledge_role` ( `role_id`, `knowledge_id`, `team_id`,  `doc_id`, `dataset_id`) VALUES (100, {item["id"]}, {item['team_id']}, {item['doc_id']}, {item['dataset_id']});'''
    cursor.execute(sql)
In [ ]:
values = [f'''(200, {item["id"]}, {item['team_id']}, {item['doc_id']}, {item['dataset_id']})''' for item in table if item["team_id"]==106]
# cursor.execute(sql)
In [30]:
sql = f'''INSERT INTO `school_server`.`robot_knowledge_role` ( `role_id`, `knowledge_id`, `team_id`,  `doc_id`, `dataset_id`) VALUES {values};'''
In [33]:
sql
Out [33]:
'INSERT INTO `school_server`.`robot_knowledge_role` ( `role_id`, `knowledge_id`, `team_id`,  `doc_id`, `dataset_id`) VALUES (200, 1, 106, 7493798895450538011, 7484070579806076980),(200, 2, 106, 7493793640993783820, 7484070579806076980),(200, 3, 106, 7487788096340541490, 7484070579806076980),(200, 4, 106, 7493831983656599588, 7484070579806076980),(200, 5, 106, 7493829638301581346, 7484070579806076980),(200, 6, 106, 7493833309840638002, 7484070579806076980),(200, 7, 106, 7493831831671881740, 7484070579806076980),(200, 8, 106, 7504958246848069695, 7484070579806076980),(200, 9, 106, 7493831043427254287, 7484070579806076980),(200, 10, 106, 7493828500946026523, 7484070579806076980),(200, 12, 106, 7493830959789097010, 7484070579806076980),(200, 14, 106, 7493828993298759680, 7484070579806076980),(200, 15, 106, 7493828981784281140, 7484070579806076980),(200, 16, 106, 7493828981575532559, 7484070579806076980),(200, 17, 106, 7493826379621793801, 7484070579806076980),(200, 18, 106, 7493829827553738786, 7484070579806076980),(200, 19, 106, 7493827900854468623, 7484070579806076980),(200, 64, 106, 7504123255642325000, 7484070579806077000),(200, 170, 106, 7504929805932019712, 7484070579806076980),(200, 171, 106, 7504930636316295168, 7484070579806076980),(200, 173, 106, 7504929616852451391, 7484070579806076980),(200, 174, 106, 7504929523592151066, 7484070579806076980),(200, 175, 106, 7504929527064281097, 7484070579806076980),(200, 176, 106, 7504929527064330249, 7484070579806076980),(200, 177, 106, 7504932503968333862, 7484070579806076980),(200, 179, 106, 7504933626712440832, 7484070579806076980),(200, 180, 106, 7504934665473409035, 7484070579806076980),(200, 181, 106, 7504934665683189801, 7484070579806076980),(200, 182, 106, 7504941148646817844, 7484070579806076980),(200, 183, 106, 7504940351712313359, 7484070579806076980),(200, 188, 106, 7504941839004090420, 7484070579806076980),(200, 192, 106, 7504947533727580186, 7484070579806076980),(200, 193, 106, 7504948407497916431, 7484070579806076980),(200, 208, 106, 7504960381623058441, 7484070579806076980),(200, 217, 106, 7504961143317561384, 7484070579806076980),(200, 220, 106, 7504965659202748451, 7484070579806076980),(200, 227, 106, 7504967980234129442, 7484070579806076980),(200, 228, 106, 7504969293270122511, 7484070579806076980),(200, 229, 106, 7504968994516893748, 7484070579806076980),(200, 230, 106, 7504969659939864585, 7484070579806076980),(200, 231, 106, 7504970197435220018, 7484070579806076980),(200, 232, 106, 7504969659939913737, 7484070579806076980),(200, 233, 106, 7504969843867697171, 7484070579806076980),(200, 234, 106, 7504969893033623567, 7484070579806076980),(200, 235, 106, 7504969659939979273, 7484070579806076980),(200, 236, 106, 7504968426901897216, 7484070579806076980),(200, 237, 106, 7504970197641560090, 7484070579806076980),(200, 238, 106, 7504969844489076748, 7484070579806076980),(200, 239, 106, 7504969873518788646, 7484070579806076980),(200, 240, 106, 7504970367069437963, 7484070579806076980),(200, 241, 106, 7504968426902093824, 7484070579806076980),(200, 242, 106, 7504968426902110208, 7484070579806076980),(200, 243, 106, 7504969844489125900, 7484070579806076980),(200, 244, 106, 7504970197641674778, 7484070579806076980),(200, 245, 106, 7504969893034180623, 7484070579806076980),(200, 246, 106, 7504969907315572776, 7484070579806076980),(200, 247, 106, 7504968426902290432, 7484070579806076980),(200, 248, 106, 7504971290529251354, 7484070579806076980),(200, 249, 106, 7504968994517516340, 7484070579806076980),(200, 250, 106, 7504972149074559010, 7484070579806076980),(200, 251, 106, 7504970954980769819, 7484070579806076980),(200, 252, 106, 7504972243660079138, 7484070579806076980),(200, 253, 106, 7504972243660095522, 7484070579806076980),(200, 254, 106, 7504971459249750028, 7484070579806076980),(200, 255, 106, 7504970300418490409, 7484070579806076980),(200, 256, 106, 7504972579393306665, 7484070579806076980),(200, 257, 106, 7504972243660701730, 7484070579806076980),(200, 258, 106, 7504972243660734498, 7484070579806076980),(200, 259, 106, 7504973775029485577, 7484070579806076980),(200, 260, 106, 7504973222212812837, 7484070579806076980),(200, 261, 106, 7504969293744029696, 7484070579806076980),(200, 262, 106, 7504973436738158633, 7484070579806076980),(200, 263, 106, 7504974642226954276, 7484070579806076980),(200, 264, 106, 7504973775029747721, 7484070579806076980),(200, 265, 106, 7504973775029780489, 7484070579806076980),(200, 266, 106, 7504972244134395938, 7484070579806076980),(200, 267, 106, 7504972581586894875, 7484070579806076980),(200, 268, 106, 7504972581586911259, 7484070579806076980),(200, 269, 106, 7504975438927724584, 7484070579806076980),(200, 270, 106, 7504975438927740968, 7484070579806076980),(200, 271, 106, 7504976476929556530, 7484070579806076980),(200, 272, 106, 7504975868206350347, 7484070579806076980),(200, 273, 106, 7504979646199480330, 7484070579806076980),(200, 274, 106, 7504975438927773736, 7484070579806076980),(200, 275, 106, 7504972685018972212, 7484070579806076980),(200, 276, 106, 7504974960579035151, 7484070579806076980),(200, 277, 106, 7504972685018988596, 7484070579806076980),(200, 278, 106, 7504972685019021364, 7484070579806076980),(200, 279, 106, 7504972685019037748, 7484070579806076980),(200, 280, 106, 7504975438927855656, 7484070579806076980),(200, 281, 106, 7504976003066200074, 7484070579806076980),(200, 282, 106, 7504974960579051535, 7484070579806076980),(200, 283, 106, 7504975438927872040, 7484070579806076980),(200, 284, 106, 7504975438927888424, 7484070579806076980),(200, 285, 106, 7504974960579248143, 7484070579806076980),(200, 286, 106, 7504972685019070516, 7484070579806076980),(200, 287, 106, 7504975438927904808, 7484070579806076980),(200, 288, 106, 7504972685019086900, 7484070579806076980),(200, 289, 106, 7504979646199644170, 7484070579806076980),(200, 290, 106, 7504972685019103284, 7484070579806076980),(200, 291, 106, 7504979008561020991, 7484070579806076980),(200, 292, 106, 7504975906710503465, 7484070579806076980),(200, 293, 106, 7504978010504904740, 7484070579806076980),(200, 294, 106, 7504979008561053759, 7484070579806076980),(200, 295, 106, 7504976476929769522, 7484070579806076980),(200, 296, 106, 7504972685019136052, 7484070579806076980),(200, 297, 106, 7504972685019152436, 7484070579806076980),(200, 298, 106, 7504972685019168820, 7484070579806076980),(200, 299, 106, 7504976003066265610, 7484070579806076980),(200, 300, 106, 7504975438928019496, 7484070579806076980),(200, 301, 106, 7504976476929785906, 7484070579806076980),(200, 302, 106, 7504975438928035880, 7484070579806076980),(200, 303, 106, 7504972685019201588, 7484070579806076980),(200, 304, 106, 7504972685019217972, 7484070579806076980),(200, 305, 106, 7504974960579330063, 7484070579806076980),(200, 306, 106, 7504978181490049039, 7484070579806076980),(200, 307, 106, 7504974960579362831, 7484070579806076980),(200, 308, 106, 7504976476929933362, 7484070579806076980),(200, 309, 106, 7504979008561135679, 7484070579806076980),(200, 310, 106, 7504979008561152063, 7484070579806076980),(200, 311, 106, 7504975439128903714, 7484070579806076980),(200, 329, 106, 7506421855394955327, 7484070579806076980),(200, 336, 106, 7514572124083650594, 7484070579806076980),(200, 337, 106, 7515018357037957172, 7484070579806076980),(200, 338, 106, 7521946826728423462, 7484070579806076980),(200, 339, 106, 7522316877478953012, 7484070579806076980),(200, 340, 106, 7522673039637561382, 7484070579806076980),(200, 341, 106, 7532759381411610624, 7484070579806076980),(200, 342, 106, 7535307477702934571, 7484070579806076980),(200, 343, 106, 7535306311941701641, 7484070579806076980),(200, 344, 106, 7535366138773078050, 7484070579806076980);'
In [ ]:
INSERT INTO `school_server`.`robot_knowledge_role` (`id`, `role_id`, `knowledge_id`, `team_id`, `create_time`, `update_time`, `doc_id`, `dataset_id`) VALUES (47, 999, 84, 106, '2025-11-03 16:52:53', '2025-11-03 16:52:53', 7568416160399245339, NULL);
---------------------------------------------------------------------------
InterfaceError                            Traceback (most recent call last)
Cell In[24], line 1
----> 1 cursor.execute(sql)

File d:\Anaconda3\Lib\site-packages\pymysql\cursors.py:153, in Cursor.execute(self, query, args)
    149     pass
    151 query = self.mogrify(query, args)
--> 153 result = self._query(query)
    154 self._executed = query
    155 return result

File d:\Anaconda3\Lib\site-packages\pymysql\cursors.py:322, in Cursor._query(self, q)
    320 conn = self._get_db()
    321 self._clear_result()
--> 322 conn.query(q)
    323 self._do_get_result()
    324 return self.rowcount

File d:\Anaconda3\Lib\site-packages\pymysql\connections.py:562, in Connection.query(self, sql, unbuffered)
    560 if isinstance(sql, str):
    561     sql = sql.encode(self.encoding, "surrogateescape")
--> 562 self._execute_command(COMMAND.COM_QUERY, sql)
    563 self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    564 return self._affected_rows

File d:\Anaconda3\Lib\site-packages\pymysql\connections.py:843, in Connection._execute_command(self, command, sql)
    838 """
    839 :raise InterfaceError: If the connection is closed.
    840 :raise ValueError: If no username was specified.
    841 """
    842 if not self._sock:
--> 843     raise err.InterfaceError(0, "")
    845 # If the last query was unbuffered, make sure it finishes before
    846 # sending new commands
    847 if self._result is not None:

InterfaceError: (0, '')
In [ ]:
connection.commit()