mdbq 3.3.3__py3-none-any.whl → 3.3.5__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/aggregation/query_data.py +2 -2
- mdbq/mysql/mysql.py +90 -16
- {mdbq-3.3.3.dist-info → mdbq-3.3.5.dist-info}/METADATA +1 -1
- {mdbq-3.3.3.dist-info → mdbq-3.3.5.dist-info}/RECORD +6 -6
- {mdbq-3.3.3.dist-info → mdbq-3.3.5.dist-info}/WHEEL +1 -1
- {mdbq-3.3.3.dist-info → mdbq-3.3.5.dist-info}/top_level.txt +0 -0
mdbq/aggregation/query_data.py
CHANGED
@@ -1253,8 +1253,8 @@ class MysqlDatasQuery:
|
|
1253
1253
|
'三级来源索引': 'smallint',
|
1254
1254
|
}
|
1255
1255
|
# df.to_csv('/Users/xigua/Downloads/ll.csv', index=False, header=True, encoding='utf-8_sig')
|
1256
|
-
min_date = df['日期'].min()
|
1257
|
-
max_date = df['日期'].max()
|
1256
|
+
min_date = df['日期'].min().strftime("%Y-%m-%d")
|
1257
|
+
max_date = df['日期'].max().strftime("%Y-%m-%d")
|
1258
1258
|
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
1259
1259
|
print(f'{now} 正在更新: mysql ({host}:{port}) {db_name}/{table_name} -> {min_date}~{max_date}')
|
1260
1260
|
m_engine.df_to_mysql(
|
mdbq/mysql/mysql.py
CHANGED
@@ -128,6 +128,19 @@ class MysqlUpload:
|
|
128
128
|
|
129
129
|
return wrapper
|
130
130
|
|
131
|
+
def keep_connect(self, _db_name, _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(20)
|
141
|
+
print(f'{_db_name}: 连接失败,重试次数超限,当前设定次数: {max_try}')
|
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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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, _db_name, _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(20)
|
1253
|
+
print(f'{_db_name}: 连接失败,重试次数超限,当前设定次数: {max_try}')
|
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(_db_name=self.db_name, _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(_db_name=self.db_name, _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(_db_name=self.db_name, _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(_db_name=db_name, _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(_db_name=db_name, _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(_db_name=self.db_name, _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()
|
@@ -5,7 +5,7 @@ mdbq/aggregation/aggregation.py,sha256=-yzApnlqSN2L0E1YMu5ml-W827qpKQvWPCOI7jj2k
|
|
5
5
|
mdbq/aggregation/df_types.py,sha256=U9i3q2eRPTDY8qAPTw7irzu-Tlg4CIySW9uYro81wdk,8125
|
6
6
|
mdbq/aggregation/mysql_types.py,sha256=YTGyrF9vcRgfkQbpT-e-JdJ7c7VF1dDHgyx9YZRES8w,10934
|
7
7
|
mdbq/aggregation/optimize_data.py,sha256=RXIv7cACCgYyehAxMjUYi_S7rVyjIwXKWMaM3nduGtA,3068
|
8
|
-
mdbq/aggregation/query_data.py,sha256=
|
8
|
+
mdbq/aggregation/query_data.py,sha256=_5mnSFHV6xAFs_1YF_H2zMOdJeMavgga4lZQ_qpqxPQ,167637
|
9
9
|
mdbq/bdup/__init__.py,sha256=AkhsGk81SkG1c8FqDH5tRq-8MZmFobVbN60DTyukYTY,28
|
10
10
|
mdbq/bdup/bdup.py,sha256=LAV0TgnQpc-LB-YuJthxb0U42_VkPidzQzAagan46lU,4234
|
11
11
|
mdbq/clean/__init__.py,sha256=A1d6x3L27j4NtLgiFV5TANwEkLuaDfPHDQNrPBbNWtU,41
|
@@ -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=
|
29
|
+
mdbq/mysql/mysql.py,sha256=ZK6E-idQWrURtoimc0uG8B1tnhtSFQXDJHfu8sWeJg4,85675
|
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.
|
48
|
-
mdbq-3.3.
|
49
|
-
mdbq-3.3.
|
50
|
-
mdbq-3.3.
|
47
|
+
mdbq-3.3.5.dist-info/METADATA,sha256=ndDUyzoqoEH--Mhw_0Z7xXEPTwWuLyBkT8jsfUx8AxA,243
|
48
|
+
mdbq-3.3.5.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
49
|
+
mdbq-3.3.5.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
|
50
|
+
mdbq-3.3.5.dist-info/RECORD,,
|
File without changes
|