Skip to content

Commit bc754de

Browse files
committed
add: add MySQL.py
1 parent e296057 commit bc754de

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

extendingPython/SQLs/MySQL.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pymysql
2+
3+
4+
class SQLConn:
5+
def __init__(self, host, port, user, password, db):
6+
"""
7+
这个函数用来连接数据库,由使用者传入数据库地址、用户名、密码、数据库名称 \n
8+
:param host: str, 数据库地址 \n
9+
:param user: str, 数据库用户名 \n
10+
:param password: str, 数据库密码 \n
11+
:param db: str, 数据库名称
12+
"""
13+
self.db = db
14+
self.password = password
15+
self.user = user
16+
self.host = host
17+
self.port = port
18+
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.db)
19+
20+
def runSelect(self, sql):
21+
"""
22+
这个函数用来执行查询操作,由使用者传入sql语句,返回查询结果 \n
23+
:param sql: str, sql语句 \n
24+
:return: 查询结果,返回一个列表,每个元素是一个字典,表示一条记录
25+
"""
26+
cur = self.conn.cursor()
27+
cur.execute(sql)
28+
sqlData = cur.fetchall()
29+
cur.close()
30+
self.conn.close()
31+
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.db)
32+
return sqlData
33+
34+
def runUpdate(self, sql):
35+
"""
36+
这个函数用来执行更新操作,比如插入、删除、更新等 \n
37+
:param sql: str, sql语句 \n
38+
:return: bool值,表示是否更新成功
39+
"""
40+
cur = self.conn.cursor()
41+
cur.execute(sql)
42+
cur.close()
43+
self.conn.commit()
44+
self.conn.close()
45+
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.db)

extendingPython/SQLs/__init__.py

Whitespace-only changes.

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
setuptools~=68.2.0
1+
setuptools==70.0.0
22
pyhttpx~=2.10.12
3-
requests~=2.32.3
3+
requests~=2.32.3
4+
pymysql~=1.1.0

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
install_requires=[
1414
# 列出你的包依赖,例如:
1515
# 'requests>=2.25.1',
16-
'setuptools~=68.2.0',
16+
"setuptools==70.0.0",
1717
'pyhttpx~=2.10.12',
18-
'requests~=2.32.3'
18+
'requests~=2.32.3',
19+
'pymysql~=1.1.0'
1920
],
2021
classifiers=[
2122
'Programming Language :: Python :: 3',

0 commit comments

Comments
 (0)