mdbq 3.3.3__py3-none-any.whl → 3.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mdbq/mysql/mysql.py CHANGED
@@ -128,6 +128,19 @@ class MysqlUpload:
128
128
 
129
129
  return wrapper
130
130
 
131
+ def keep_connect(self, _config, max_try: int=5):
132
+ attempts = 1
133
+ while attempts <= max_try:
134
+ try:
135
+ connection = pymysql.connect(**_config) # 连接数据库
136
+ return connection
137
+ except Exception as e:
138
+ print(f'连接失败,正在重试: {attempts}/{max_try} {e}')
139
+ attempts += 1
140
+ time.sleep(10)
141
+ print(f'连接失败,重试次数超限')
142
+ return None
143
+
131
144
  def cover_doc_dtypes(self, dict_data):
132
145
  """ 清理字典键值 并转换数据类型 """
133
146
  if not dict_data:
@@ -201,7 +214,10 @@ class MysqlUpload:
201
214
  print(f'dict_data 中"数据主体"键不能为空')
202
215
  return
203
216
 
204
- connection = pymysql.connect(**self.config) # 连接数据库
217
+ # connection = pymysql.connect(**self.config) # 连接数据库
218
+ connection = self.keep_connect(_config=self.config, max_try=5)
219
+ if not connection:
220
+ return
205
221
  with connection.cursor() as cursor:
206
222
  cursor.execute(f"SHOW DATABASES LIKE '{db_name}'") # 检查数据库是否存在
207
223
  database_exists = cursor.fetchone()
@@ -219,7 +235,10 @@ class MysqlUpload:
219
235
  print(f"创建Database: {db_name}")
220
236
 
221
237
  self.config.update({'database': db_name}) # 添加更新 config 字段
222
- connection = pymysql.connect(**self.config) # 重新连接数据库
238
+ # connection = pymysql.connect(**self.config) # 重新连接数据库
239
+ connection = self.keep_connect(_config=self.config, max_try=5)
240
+ if not connection:
241
+ return
223
242
  with connection.cursor() as cursor:
224
243
  # 1. 查询表, 不存在则创建一个空表
225
244
  sql = "SHOW TABLES LIKE %s;" # 有特殊字符不需转义
@@ -362,7 +381,10 @@ class MysqlUpload:
362
381
  except Exception as e:
363
382
  print(f'{table_name} 将数据按年/月保存(cut_data),但在转换日期时报错 -> {e}')
364
383
 
365
- connection = pymysql.connect(**self.config) # 连接数据库
384
+ # connection = pymysql.connect(**self.config) # 连接数据库
385
+ connection = self.keep_connect(_config=self.config, max_try=5)
386
+ if not connection:
387
+ return
366
388
  with connection.cursor() as cursor:
367
389
  cursor.execute(f"SHOW DATABASES LIKE '{db_name}'") # 检查数据库是否存在
368
390
  database_exists = cursor.fetchone()
@@ -380,7 +402,10 @@ class MysqlUpload:
380
402
  print(f"创建Database: {db_name}")
381
403
 
382
404
  self.config.update({'database': db_name}) # 添加更新 config 字段
383
- connection = pymysql.connect(**self.config) # 重新连接数据库
405
+ # connection = pymysql.connect(**self.config) # 重新连接数据库
406
+ connection = self.keep_connect(_config=self.config, max_try=5)
407
+ if not connection:
408
+ return
384
409
  with connection.cursor() as cursor:
385
410
  # 1. 查询表, 不存在则创建一个空表
386
411
  sql = "SHOW TABLES LIKE %s;" # 有特殊字符不需转义
@@ -723,7 +748,10 @@ class MysqlUpload:
723
748
  # 确保传进来的 set_typ 键存在于实际的 df 列才 update
724
749
  [dtypes.update({k: inside_v}) for inside_k, inside_v in set_typ.items() if k == inside_k]
725
750
 
726
- connection = pymysql.connect(**self.config) # 连接数据库
751
+ # connection = pymysql.connect(**self.config) # 连接数据库
752
+ connection = self.keep_connect(_config=self.config, max_try=5)
753
+ if not connection:
754
+ return
727
755
  with connection.cursor() as cursor:
728
756
  cursor.execute(f"SHOW DATABASES LIKE '{db_name}'") # 检查数据库是否存在
729
757
  database_exists = cursor.fetchone()
@@ -741,7 +769,10 @@ class MysqlUpload:
741
769
  print(f"创建Database: {db_name}")
742
770
 
743
771
  self.config.update({'database': db_name}) # 添加更新 config 字段
744
- connection = pymysql.connect(**self.config) # 重新连接数据库
772
+ # connection = pymysql.connect(**self.config) # 重新连接数据库
773
+ connection = self.keep_connect(_config=self.config, max_try=5)
774
+ if not connection:
775
+ return
745
776
  with connection.cursor() as cursor:
746
777
  # 1. 查询表, 不存在则创建一个空表
747
778
  sql = "SHOW TABLES LIKE %s;" # 有特殊字符不需转义
@@ -1017,7 +1048,10 @@ class MysqlUpload:
1017
1048
  if not filename:
1018
1049
  print(f'未指定文件名: filename')
1019
1050
  return
1020
- connection = pymysql.connect(**self.config) # 连接数据库
1051
+ # connection = pymysql.connect(**self.config) # 连接数据库
1052
+ connection = self.keep_connect(_config=self.config, max_try=5)
1053
+ if not connection:
1054
+ return
1021
1055
  # try:
1022
1056
  with connection.cursor() as cursor:
1023
1057
  cursor.execute(f"SHOW DATABASES LIKE '{db_name}'") # 检查数据库是否存在
@@ -1026,7 +1060,10 @@ class MysqlUpload:
1026
1060
  print(f"Database {db_name} 数据库不存在")
1027
1061
  return
1028
1062
  self.config.update({'database': db_name})
1029
- connection = pymysql.connect(**self.config) # 重新连接数据库
1063
+ # connection = pymysql.connect(**self.config) # 重新连接数据库
1064
+ connection = self.keep_connect(_config=self.config, max_try=5)
1065
+ if not connection:
1066
+ return
1030
1067
  with connection.cursor() as cursor:
1031
1068
  # 1. 查询表
1032
1069
  sql = "SHOW TABLES LIKE %s;" # 有特殊字符不需转义
@@ -1056,7 +1093,10 @@ class MysqlUpload:
1056
1093
  end_date = pd.to_datetime(end_date).strftime('%Y-%m-%d')
1057
1094
  df = pd.DataFrame()
1058
1095
 
1059
- connection = pymysql.connect(**self.config) # 连接数据库
1096
+ # connection = pymysql.connect(**self.config) # 连接数据库
1097
+ connection = self.keep_connect(_config=self.config, max_try=5)
1098
+ if not connection:
1099
+ return
1060
1100
  try:
1061
1101
  with connection.cursor() as cursor:
1062
1102
  cursor.execute(f"SHOW DATABASES LIKE '{db_name}'") # 检查数据库是否存在
@@ -1075,7 +1115,10 @@ class MysqlUpload:
1075
1115
  before_time = time.time()
1076
1116
  # 读取数据
1077
1117
  self.config.update({'database': db_name})
1078
- connection = pymysql.connect(**self.config) # 重新连接数据库
1118
+ # connection = pymysql.connect(**self.config) # 重新连接数据库
1119
+ connection = self.keep_connect(_config=self.config, max_try=5)
1120
+ if not connection:
1121
+ return
1079
1122
  try:
1080
1123
  with connection.cursor() as cursor:
1081
1124
  # 获取指定日期范围的数据
@@ -1197,6 +1240,19 @@ class OptimizeDatas:
1197
1240
 
1198
1241
  return wrapper
1199
1242
 
1243
+ def keep_connect(self, _config, max_try: int=5):
1244
+ attempts = 1
1245
+ while attempts <= max_try:
1246
+ try:
1247
+ connection = pymysql.connect(**_config) # 连接数据库
1248
+ return connection
1249
+ except Exception as e:
1250
+ print(f'连接失败,正在重试: {attempts}/{max_try} {e}')
1251
+ attempts += 1
1252
+ time.sleep(10)
1253
+ print(f'连接失败,重试次数超限')
1254
+ return None
1255
+
1200
1256
  def optimize_list(self):
1201
1257
  """
1202
1258
  更新多个数据库 移除冗余数据
@@ -1243,7 +1299,10 @@ class OptimizeDatas:
1243
1299
  # if '店铺指标' not in table_name:
1244
1300
  # continue
1245
1301
  self.config.update({'database': self.db_name}) # 添加更新 config 字段
1246
- self.connection = pymysql.connect(**self.config)
1302
+ # self.connection = pymysql.connect(**self.config)
1303
+ self.connection = self.keep_connect(_config=self.config, max_try=5)
1304
+ if not self.connection:
1305
+ return
1247
1306
  with self.connection.cursor() as cursor:
1248
1307
  sql = f"SELECT 1 FROM `{table_name}` LIMIT 1"
1249
1308
  # print(sql)
@@ -1395,7 +1454,10 @@ class OptimizeDatas:
1395
1454
 
1396
1455
  def database_list(self):
1397
1456
  """ 获取所有数据库 """
1398
- connection = pymysql.connect(**self.config) # 连接数据库
1457
+ # connection = pymysql.connect(**self.config) # 连接数据库
1458
+ connection = self.keep_connect(_config=self.config, max_try=5)
1459
+ if not connection:
1460
+ return
1399
1461
  with connection.cursor() as cursor:
1400
1462
  cursor.execute("SHOW DATABASES")
1401
1463
  databases = cursor.fetchall() # 获取所有数据库的结果
@@ -1404,7 +1466,10 @@ class OptimizeDatas:
1404
1466
 
1405
1467
  def table_list(self, db_name):
1406
1468
  """ 获取指定数据库的所有数据表 """
1407
- connection = pymysql.connect(**self.config) # 连接数据库
1469
+ # connection = pymysql.connect(**self.config) # 连接数据库
1470
+ connection = self.keep_connect(_config=self.config, max_try=5)
1471
+ if not connection:
1472
+ return
1408
1473
  try:
1409
1474
  with connection.cursor() as cursor:
1410
1475
  cursor.execute(f"SHOW DATABASES LIKE '{db_name}'") # 检查数据库是否存在
@@ -1420,7 +1485,10 @@ class OptimizeDatas:
1420
1485
  connection.close() # 断开连接
1421
1486
 
1422
1487
  self.config.update({'database': db_name}) # 添加更新 config 字段
1423
- connection = pymysql.connect(**self.config) # 重新连接数据库
1488
+ # connection = pymysql.connect(**self.config) # 重新连接数据库
1489
+ connection = self.keep_connect(_config=self.config, max_try=5)
1490
+ if not connection:
1491
+ return
1424
1492
  with connection.cursor() as cursor:
1425
1493
  cursor.execute("SHOW TABLES")
1426
1494
  tables = cursor.fetchall() # 获取所有数据表
@@ -1432,7 +1500,10 @@ class OptimizeDatas:
1432
1500
  获取指定数据表的数据, 按天获取
1433
1501
  """
1434
1502
  self.config.update({'database': db_name}) # 添加更新 config 字段
1435
- connection = pymysql.connect(**self.config)
1503
+ # connection = pymysql.connect(**self.config)
1504
+ connection = self.keep_connect(_config=self.config, max_try=5)
1505
+ if not connection:
1506
+ return
1436
1507
  try:
1437
1508
  with connection.cursor() as cursor:
1438
1509
  sql = f"SELECT * FROM `{table_name}` WHERE {'日期'} BETWEEN '%s' AND '%s'" % (date, date)
@@ -1465,7 +1536,10 @@ class OptimizeDatas:
1465
1536
  for table_dict in tables:
1466
1537
  for key, table_name in table_dict.items():
1467
1538
  self.config.update({'database': self.db_name}) # 添加更新 config 字段
1468
- self.connection = pymysql.connect(**self.config)
1539
+ # self.connection = pymysql.connect(**self.config)
1540
+ self.connection = self.keep_connect(_config=self.config, max_try=5)
1541
+ if not self.connection:
1542
+ return
1469
1543
  with self.connection.cursor() as cursor:
1470
1544
  cursor.execute(f"SHOW FULL COLUMNS FROM `{table_name}`") # 查询数据表的列信息
1471
1545
  columns = cursor.fetchall()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 3.3.3
3
+ Version: 3.3.4
4
4
  Home-page: https://pypi.org/project/mdbq
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -26,7 +26,7 @@ mdbq/log/mylogger.py,sha256=oaT7Bp-Hb9jZt52seP3ISUuxVcI19s4UiqTeouScBO0,3258
26
26
  mdbq/mongo/__init__.py,sha256=SILt7xMtQIQl_m-ik9WLtJSXIVf424iYgCfE_tnQFbw,13
27
27
  mdbq/mongo/mongo.py,sha256=v9qvrp6p1ZRWuPpbSilqveiE0FEcZF7U5xUPI0RN4xs,31880
28
28
  mdbq/mysql/__init__.py,sha256=A_DPJyAoEvTSFojiI2e94zP0FKtCkkwKP1kYUCSyQzo,11
29
- mdbq/mysql/mysql.py,sha256=dpQWQTq8PpSYFPAHZRNinLuHKfrekSNYbyOxtOD0SwA,82319
29
+ mdbq/mysql/mysql.py,sha256=pTeZD0KgVNJnRLaMXP11wz8lBBq2_xkAqIuoRmnEMWA,85259
30
30
  mdbq/mysql/recheck_mysql.py,sha256=rgTpvDMWYTyEn7UQdlig-pdXDluTgiU8JG6lkMh8DV0,8665
31
31
  mdbq/mysql/s_query.py,sha256=MbIprZ4yJDAZ9AahZPzl7hqS695Vs0P-AJNwAtA_EEc,9287
32
32
  mdbq/mysql/year_month_day.py,sha256=VgewoE2pJxK7ErjfviL_SMTN77ki8GVbTUcao3vFUCE,1523
@@ -44,7 +44,7 @@ mdbq/req_post/__init__.py,sha256=jso1oHcy6cJEfa7udS_9uO5X6kZLoPBF8l3wCYmr5dM,18
44
44
  mdbq/req_post/req_tb.py,sha256=qg7pet73IgKGmCwxaeUyImJIoeK_pBQT9BBKD7fkBNg,36160
45
45
  mdbq/spider/__init__.py,sha256=RBMFXGy_jd1HXZhngB2T2XTvJqki8P_Fr-pBcwijnew,18
46
46
  mdbq/spider/aikucun.py,sha256=BKVa0xbTkyhIH5kQgOdyPDtwFPScbMNAFYup_-fFF9Y,24809
47
- mdbq-3.3.3.dist-info/METADATA,sha256=2_AvXdqYGWMuln2wfqXfLKddw2h8Sbr49mK1UjEiiVU,243
48
- mdbq-3.3.3.dist-info/WHEEL,sha256=cpQTJ5IWu9CdaPViMhC9YzF8gZuS5-vlfoFihTBC86A,91
49
- mdbq-3.3.3.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
50
- mdbq-3.3.3.dist-info/RECORD,,
47
+ mdbq-3.3.4.dist-info/METADATA,sha256=5HCgLOtVbO2JXkq0HhNkTOo2wqWf4MFVh78NWT_ruPk,243
48
+ mdbq-3.3.4.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
49
+ mdbq-3.3.4.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
50
+ mdbq-3.3.4.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.1.0)
2
+ Generator: bdist_wheel (0.44.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5