cppackage 0.2.0__tar.gz → 0.2.2__tar.gz
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.
- cppackage-0.2.2/CPpackage/db/__init__.py +2 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage/db/sql_model.py +10 -27
- cppackage-0.2.2/CPpackage/db/sql_modelcopy.py +105 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/PKG-INFO +9 -7
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/SOURCES.txt +1 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/PKG-INFO +9 -7
- {cppackage-0.2.0 → cppackage-0.2.2}/readme.md +8 -6
- {cppackage-0.2.0 → cppackage-0.2.2}/setup.py +1 -1
- cppackage-0.2.0/CPpackage/db/__init__.py +0 -2
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage/__init__.py +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage/core/__init__.py +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage/db/config.py +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage/utils/__init__.py +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/dependency_links.txt +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/entry_points.txt +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/not-zip-safe +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/requires.txt +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/CPpackage.egg-info/top_level.txt +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/LICENSE +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/MANIFEST.in +0 -0
- {cppackage-0.2.0 → cppackage-0.2.2}/setup.cfg +0 -0
|
@@ -32,7 +32,7 @@ def _get_connection(database=None, port=None):
|
|
|
32
32
|
)
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
# =====================
|
|
35
|
+
# ===================== 查询操作 =====================
|
|
36
36
|
|
|
37
37
|
def sel_data(sql, params=None, port=None, database=None):
|
|
38
38
|
|
|
@@ -45,32 +45,16 @@ def sel_data(sql, params=None, port=None, database=None):
|
|
|
45
45
|
except pymysql.MySQLError as e:
|
|
46
46
|
print("查询失败:", e)
|
|
47
47
|
return None
|
|
48
|
+
|
|
49
|
+
# ===================== 增量更新 =====================
|
|
50
|
+
def incremental_update(sql, values, port=None, database=None):
|
|
51
|
+
with _get_connection(database=database, port=port) as conn:
|
|
52
|
+
with conn.cursor() as cursor:
|
|
53
|
+
cursor.executemany(sql, values)
|
|
54
|
+
conn.commit()
|
|
48
55
|
|
|
49
56
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
try:
|
|
53
|
-
with _get_connection(database, port) as conn:
|
|
54
|
-
with conn.cursor() as cursor:
|
|
55
|
-
cursor.execute(sql, values)
|
|
56
|
-
conn.commit()
|
|
57
|
-
|
|
58
|
-
except pymysql.MySQLError as e:
|
|
59
|
-
print("更新失败:", e)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def update_datas(sql, values, port=None, database=None):
|
|
63
|
-
|
|
64
|
-
try:
|
|
65
|
-
with _get_connection(database, port) as conn:
|
|
66
|
-
with conn.cursor() as cursor:
|
|
67
|
-
cursor.executemany(sql, values)
|
|
68
|
-
conn.commit()
|
|
69
|
-
|
|
70
|
-
except pymysql.MySQLError as e:
|
|
71
|
-
print("批量更新失败:", e)
|
|
72
|
-
|
|
73
|
-
|
|
57
|
+
# ===================== 删除操作 =====================
|
|
74
58
|
def del_data(sql, params=None, port=None, database=None):
|
|
75
59
|
|
|
76
60
|
try:
|
|
@@ -151,13 +135,12 @@ def check_and_sync_columns(df, table_name, database, max_new=5):
|
|
|
151
135
|
|
|
152
136
|
# ===================== DataFrame 入库 =====================
|
|
153
137
|
|
|
154
|
-
def
|
|
138
|
+
def update_datas(df, table_name, database):
|
|
155
139
|
|
|
156
140
|
conn = None
|
|
157
141
|
|
|
158
142
|
try:
|
|
159
143
|
# 表结构校验
|
|
160
|
-
check_and_sync_columns(df, table_name, database)
|
|
161
144
|
conn = _get_connection(database)
|
|
162
145
|
cursor = conn.cursor()
|
|
163
146
|
cols = list(df.columns)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import pymysql
|
|
2
|
+
import time
|
|
3
|
+
from pymysql import Error
|
|
4
|
+
from .config import get_db_config
|
|
5
|
+
|
|
6
|
+
def _get_connection(database=None, port=None):
|
|
7
|
+
cfg = get_db_config()
|
|
8
|
+
db = database if database is not None else cfg.get('database')
|
|
9
|
+
prt = port if port is not None else cfg.get('port', 3306)
|
|
10
|
+
return pymysql.connect(
|
|
11
|
+
host=cfg.get('host'),
|
|
12
|
+
user=cfg.get('user'),
|
|
13
|
+
password=cfg.get('password'),
|
|
14
|
+
database=db,
|
|
15
|
+
port=prt,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def sel_data(sql, port=None, database=None):
|
|
19
|
+
try:
|
|
20
|
+
conn = _get_connection(database=database, port=port)
|
|
21
|
+
cursor = conn.cursor()
|
|
22
|
+
cursor.execute(sql)
|
|
23
|
+
result = cursor.fetchall()
|
|
24
|
+
cursor.close()
|
|
25
|
+
conn.close()
|
|
26
|
+
return result
|
|
27
|
+
except pymysql.MySQLError as e:
|
|
28
|
+
print(f"An error occurred: {e}")
|
|
29
|
+
|
|
30
|
+
def update_data(sql, values, port=None, database=None):
|
|
31
|
+
conn = _get_connection(database=database, port=port)
|
|
32
|
+
cursor = conn.cursor()
|
|
33
|
+
cursor.execute(sql, values)
|
|
34
|
+
conn.commit()
|
|
35
|
+
cursor.close()
|
|
36
|
+
conn.close()
|
|
37
|
+
|
|
38
|
+
def updata(data, databases, table):
|
|
39
|
+
s = ""
|
|
40
|
+
st1 = ""
|
|
41
|
+
for i in data.columns.values:
|
|
42
|
+
s = s + "`" + i + "`,"
|
|
43
|
+
st1 = st1 + "%s,"
|
|
44
|
+
s = s[:-1]
|
|
45
|
+
st1 = st1[:-1]
|
|
46
|
+
all_data = []
|
|
47
|
+
for _, k in data.iterrows():
|
|
48
|
+
all_data.append(tuple(k.to_list()))
|
|
49
|
+
sql = 'INSERT INTO %s (%s) VALUES (%s)' % (table, s, st1)
|
|
50
|
+
update_datas(sql, tuple(all_data), database=databases)
|
|
51
|
+
time.sleep(2)
|
|
52
|
+
|
|
53
|
+
def deldata(all_del_list, id_name, table_name, database):
|
|
54
|
+
str11 = ""
|
|
55
|
+
for te in all_del_list:
|
|
56
|
+
if "str" in str(type(te)):
|
|
57
|
+
str11 = str11 + "'" + str(te) + "',"
|
|
58
|
+
else:
|
|
59
|
+
str11 = str11 + str(te) + ","
|
|
60
|
+
str11 = str11[:-1]
|
|
61
|
+
delsql = 'DELETE from `%s` where %s in (%s)' % (table_name, id_name, str11)
|
|
62
|
+
del_data(delsql, database=database)
|
|
63
|
+
|
|
64
|
+
def deldata_time(id_name, table_name, id, database, time_name, start_time, end_time):
|
|
65
|
+
delsql = 'DELETE from `%s` where %s = %s and `%s`>= "%s" and `%s`<= "%s"' % (
|
|
66
|
+
table_name, id_name, id, time_name, start_time, time_name, end_time
|
|
67
|
+
)
|
|
68
|
+
del_data(delsql, database=database)
|
|
69
|
+
|
|
70
|
+
def update_datas(sql, values, port=None, database=None):
|
|
71
|
+
with _get_connection(database=database, port=port) as conn:
|
|
72
|
+
with conn.cursor() as cursor:
|
|
73
|
+
cursor.executemany(sql, values)
|
|
74
|
+
conn.commit()
|
|
75
|
+
|
|
76
|
+
def del_data(sql, port=None, database=None):
|
|
77
|
+
try:
|
|
78
|
+
conn = _get_connection(database=database, port=port)
|
|
79
|
+
cursor = conn.cursor()
|
|
80
|
+
cursor.execute(sql)
|
|
81
|
+
conn.commit()
|
|
82
|
+
cursor.close()
|
|
83
|
+
conn.close()
|
|
84
|
+
except pymysql.MySQLError as e:
|
|
85
|
+
print(f"An error occurred: {e}")
|
|
86
|
+
|
|
87
|
+
def update_test(df, table_name, databases):
|
|
88
|
+
try:
|
|
89
|
+
conn = _get_connection(database=databases)
|
|
90
|
+
cursor = conn.cursor()
|
|
91
|
+
columns = ', '.join(df.columns)
|
|
92
|
+
values_template = ', '.join([f'({", ".join(["%s"] * len(df.columns))})'] * len(df))
|
|
93
|
+
update_clause = ', '.join([f'{col} = VALUES({col})' for col in df.columns if col != 'id'])
|
|
94
|
+
sql = f"INSERT INTO {table_name} ({columns}) VALUES {values_template} " \
|
|
95
|
+
f"ON DUPLICATE KEY UPDATE {update_clause}"
|
|
96
|
+
values = [tuple(row) for row in df.values]
|
|
97
|
+
flat_values = [item for sublist in values for item in sublist]
|
|
98
|
+
cursor.execute(sql, flat_values)
|
|
99
|
+
conn.commit()
|
|
100
|
+
print(f"成功插入/更新 {cursor.rowcount} 条记录")
|
|
101
|
+
except Error as e:
|
|
102
|
+
print(f"数据库错误: {e}")
|
|
103
|
+
finally:
|
|
104
|
+
if conn:
|
|
105
|
+
conn.close()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cppackage
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: 超品集团自用的Python包
|
|
5
5
|
Home-page: https://github.com/example/CPpackage
|
|
6
6
|
Author: team-数智组
|
|
@@ -40,22 +40,24 @@ CPpackage是超品集团内部使用的Python包,核心功能集中在MySQL数
|
|
|
40
40
|
## 安装
|
|
41
41
|
|
|
42
42
|
```bash
|
|
43
|
-
pip install
|
|
43
|
+
pip install cppackage
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
## 使用方法
|
|
47
47
|
|
|
48
48
|
```python
|
|
49
49
|
# 导入包
|
|
50
|
-
from CPpackage.db import set_db_config
|
|
51
|
-
from CPpackage.db.sql_model import sel_data,
|
|
50
|
+
from CPpackage.db.config import set_db_config
|
|
51
|
+
from CPpackage.db.sql_model import sel_data, del_data , incremental_update, get_table_columns,add_columns,check_and_sync_columns,update_datas
|
|
52
52
|
|
|
53
53
|
# 配置数据库连接(也可通过环境变量CPPACKAGE_DB_*配置)
|
|
54
54
|
set_db_config(host='your_host', user='your_user', password='your_password', database='your_database', port=3306)
|
|
55
55
|
|
|
56
|
-
#
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
# 更新过程会先经过check_and_sync_columns函数完成表格校验
|
|
57
|
+
check_and_sync_columns(df, table_name, database, max_new=5)
|
|
58
|
+
# 更新数据函数,在完成数据库信息配置后,df为pd.dataframe格式的数据
|
|
59
|
+
update_datas(df, table_name, database)
|
|
60
|
+
|
|
59
61
|
```
|
|
60
62
|
|
|
61
63
|
## 项目结构
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cppackage
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: 超品集团自用的Python包
|
|
5
5
|
Home-page: https://github.com/example/CPpackage
|
|
6
6
|
Author: team-数智组
|
|
@@ -40,22 +40,24 @@ CPpackage是超品集团内部使用的Python包,核心功能集中在MySQL数
|
|
|
40
40
|
## 安装
|
|
41
41
|
|
|
42
42
|
```bash
|
|
43
|
-
pip install
|
|
43
|
+
pip install cppackage
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
## 使用方法
|
|
47
47
|
|
|
48
48
|
```python
|
|
49
49
|
# 导入包
|
|
50
|
-
from CPpackage.db import set_db_config
|
|
51
|
-
from CPpackage.db.sql_model import sel_data,
|
|
50
|
+
from CPpackage.db.config import set_db_config
|
|
51
|
+
from CPpackage.db.sql_model import sel_data, del_data , incremental_update, get_table_columns,add_columns,check_and_sync_columns,update_datas
|
|
52
52
|
|
|
53
53
|
# 配置数据库连接(也可通过环境变量CPPACKAGE_DB_*配置)
|
|
54
54
|
set_db_config(host='your_host', user='your_user', password='your_password', database='your_database', port=3306)
|
|
55
55
|
|
|
56
|
-
#
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
# 更新过程会先经过check_and_sync_columns函数完成表格校验
|
|
57
|
+
check_and_sync_columns(df, table_name, database, max_new=5)
|
|
58
|
+
# 更新数据函数,在完成数据库信息配置后,df为pd.dataframe格式的数据
|
|
59
|
+
update_datas(df, table_name, database)
|
|
60
|
+
|
|
59
61
|
```
|
|
60
62
|
|
|
61
63
|
## 项目结构
|
|
@@ -7,22 +7,24 @@ CPpackage是超品集团内部使用的Python包,核心功能集中在MySQL数
|
|
|
7
7
|
## 安装
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
pip install
|
|
10
|
+
pip install cppackage
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## 使用方法
|
|
14
14
|
|
|
15
15
|
```python
|
|
16
16
|
# 导入包
|
|
17
|
-
from CPpackage.db import set_db_config
|
|
18
|
-
from CPpackage.db.sql_model import sel_data,
|
|
17
|
+
from CPpackage.db.config import set_db_config
|
|
18
|
+
from CPpackage.db.sql_model import sel_data, del_data , incremental_update, get_table_columns,add_columns,check_and_sync_columns,update_datas
|
|
19
19
|
|
|
20
20
|
# 配置数据库连接(也可通过环境变量CPPACKAGE_DB_*配置)
|
|
21
21
|
set_db_config(host='your_host', user='your_user', password='your_password', database='your_database', port=3306)
|
|
22
22
|
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
# 更新过程会先经过check_and_sync_columns函数完成表格校验
|
|
24
|
+
check_and_sync_columns(df, table_name, database, max_new=5)
|
|
25
|
+
# 更新数据函数,在完成数据库信息配置后,df为pd.dataframe格式的数据
|
|
26
|
+
update_datas(df, table_name, database)
|
|
27
|
+
|
|
26
28
|
```
|
|
27
29
|
|
|
28
30
|
## 项目结构
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|