Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
doudian-py
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
baiquan
doudian-py
Commits
4a8724ee
Commit
4a8724ee
authored
Jul 15, 2025
by
baiquan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(dao): 优化抖店账号导入功能
- 新增更新功能:在导入抖店账号时,如果环境ID已存在,则进行更新操作 - 统计并记录插入和更新的记录数- 优化代码结构,提高可读性和可维护性
parent
facd415a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
29 deletions
+59
-29
dao/db.py
+59
-29
No files found.
dao/db.py
View file @
4a8724ee
import
pandas
as
pd
from
loguru
import
logger
from
config
import
settings
import
pymysql
def
get_mysql_connection
():
"""获取 MySQL 数据库连接"""
...
...
@@ -51,23 +51,56 @@ def import_doudian_accounts_to_mysql(file_path):
password = VALUES(password),
company = VALUES(company)
"""
update_sql
=
"""
UPDATE doudian_accounts
SET environment =
%
s,
account =
%
s,
password =
%
s,
company =
%
s
WHERE environment_id =
%
s
"""
# 执行查询SQL
select_sql
=
"""
SELECT environment, account, password, company
FROM doudian_accounts
WHERE environment_id =
%
s
"""
# 遍历DataFrame并执行插入/更新
insert_count
=
0
update_count
=
0
for
_
,
row
in
df
.
iterrows
():
# 确保所有字段都有值,不能为NaN
if
pd
.
notna
(
row
[
'环境ID'
]):
data
=
(
str
(
row
[
'环境'
])
if
pd
.
notna
(
row
[
'环境'
])
else
''
,
int
(
row
[
'环境ID'
])
if
pd
.
notna
(
row
[
'环境ID'
])
else
0
,
# 假设环境ID不能为空
str
(
row
[
'账号'
])
if
pd
.
notna
(
row
[
'账号'
])
else
''
,
str
(
row
[
'密码'
])
if
pd
.
notna
(
row
[
'密码'
])
else
''
,
str
(
row
[
'公司'
])
if
pd
.
notna
(
row
[
'公司'
])
else
''
)
cursor
.
execute
(
insert_sql
,
data
)
# 确保所有字段都有值,不能为NaN
environment_id
=
int
(
row
[
'环境ID'
])
cursor
.
execute
(
select_sql
,
(
environment_id
,))
# 获取查询结果
result
=
cursor
.
fetchone
()
if
result
:
# 如果存在,则更新
update_count
+=
1
data
=
(
str
(
row
[
'环境'
])
if
pd
.
notna
(
row
[
'环境'
])
else
''
,
str
(
row
[
'账号'
])
if
pd
.
notna
(
row
[
'账号'
])
else
''
,
str
(
row
[
'密码'
])
if
pd
.
notna
(
row
[
'密码'
])
else
''
,
str
(
row
[
'公司'
])
if
pd
.
notna
(
row
[
'公司'
])
else
''
,
environment_id
)
cursor
.
execute
(
update_sql
,
data
)
else
:
insert_count
+=
1
data
=
(
str
(
row
[
'环境'
])
if
pd
.
notna
(
row
[
'环境'
])
else
''
,
int
(
row
[
'环境ID'
])
if
pd
.
notna
(
row
[
'环境ID'
])
else
0
,
# 假设环境ID不能为空
str
(
row
[
'账号'
])
if
pd
.
notna
(
row
[
'账号'
])
else
''
,
str
(
row
[
'密码'
])
if
pd
.
notna
(
row
[
'密码'
])
else
''
,
str
(
row
[
'公司'
])
if
pd
.
notna
(
row
[
'公司'
])
else
''
)
cursor
.
execute
(
insert_sql
,
data
)
print
(
data
)
# 提交事务
connection
.
commit
()
logger
.
info
(
f
"成功导入
/更新 {len(df)
} 条记录"
)
logger
.
info
(
f
"成功导入
{insert_count}/更新{update_count
} 条记录"
)
except
Exception
as
e
:
# 发生错误时回滚
...
...
@@ -77,9 +110,6 @@ def import_doudian_accounts_to_mysql(file_path):
connection
.
close
()
import
pymysql
def
find_account_by_environment_id
(
environment_id
):
"""
根据环境ID查找账号密码信息
...
...
@@ -126,18 +156,18 @@ def find_account_by_environment_id(environment_id):
connection
.
close
()
# 使用示例
if
__name__
==
"__main__"
:
# 查找环境ID为1075702795的账号
account_info
=
find_account_by_environment_id
(
1075702795
)
# 如果需要将结果用于其他处理
if
account_info
:
print
(
"
\n
获取到的账号信息:"
)
print
(
account_info
)
# # 使用示例
# if __name__ == "__main__":
# excel_file = "doudian_accounts.csv" # 你的Excel文件路径
# import_doudian_accounts_to_mysql(excel_file)
\ No newline at end of file
# # 查找环境ID为1075702795的账号
# account_info = find_account_by_environment_id(1075702795)
#
# # 如果需要将结果用于其他处理
# if account_info:
# print("\n获取到的账号信息:")
# print(account_info)
# 使用示例
if
__name__
==
"__main__"
:
excel_file
=
"./../doudian_accounts.csv"
# 你的Excel文件路径
import_doudian_accounts_to_mysql
(
excel_file
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment