Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
5aaf4c5b
Commit
5aaf4c5b
authored
Nov 12, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
ae48dbc9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
application/libs/helper/mysql.py
application/libs/helper/mysql.py
+62
-0
No files found.
application/libs/helper/mysql.py
0 → 100644
View file @
5aaf4c5b
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/11/12 11:08
# @File : mysql.py
# @Description :
"""
from
typing
import
Optional
import
pymysql
from
dbutils.pooled_db
import
PooledDB
class
MySQLHelper
:
def
__init__
(
self
,
**
kwargs
):
"""
Initialize the MySQLHelper class, create a database connection pool
"""
self
.
kwargs
=
kwargs
if
kwargs
else
{}
self
.
conn
=
None
self
.
db_pool
=
PooledDB
(
pymysql
,
**
self
.
kwargs
)
def
__enter__
(
self
):
"""
Implement the __enter__ magic method, support the with statement
"""
self
.
conn
=
self
.
db_pool
.
connection
()
return
self
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
"""
Implement the __exit__ magic method, support the with statement
"""
if
self
.
conn
:
self
.
conn
.
close
()
if
exc_type
:
print
(
f
"Error occurred:
{
exc_val
}
"
)
def
execute
(
self
,
sql
:
str
)
->
Optional
[
tuple
]:
"""
Execute the SQL statement and retrieve the results
"""
if
not
self
.
conn
:
print
(
"No connection available."
)
return
None
try
:
# Use the cursor object to execute SQL statement
with
self
.
conn
.
cursor
()
as
cursor
:
cursor
.
execute
(
sql
)
result
=
cursor
.
fetchall
()
return
result
except
Exception
as
e
:
print
(
f
"Error executing SQL:
{
e
}
"
)
return
None
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