Skip to content

Commit 7f07739

Browse files
committed
SQLs: using datebase pool
1 parent bc754de commit 7f07739

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

extendingPython/SQLs/MySQL.py

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,59 @@
11
import pymysql
2+
from dbutils.pooled_db import PooledDB
23

34

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-
"""
5+
class DatabasePool:
6+
def __init__(self, db, mincached=2, maxcached=50, maxshared=0, maxconnections=100, blocking=True, setsession=[],
7+
**config):
8+
self.config = config
139
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()
10+
self.pool = None
11+
self.mincached = mincached
12+
self.maxcached = maxcached
13+
self.maxshared = maxshared
14+
self.maxconnections = maxconnections
15+
self.blocking = blocking
16+
self.setsession = setsession
17+
18+
def createPool(self):
19+
self.pool = PooledDB(creator=pymysql, mincached=self.mincached, maxcached=self.maxcached,
20+
maxshared=self.maxshared,
21+
maxconnections=self.maxconnections, blocking=self.blocking, setsession=self.setsession,
22+
**self.config)
23+
return self.pool
24+
25+
def getConnection(self):
26+
if self.pool is None:
27+
self.createPool()
28+
return self.pool.connection()
29+
30+
def execute(self, sql):
31+
conn = self.getConnection()
32+
cur = conn.cursor()
33+
cur.execute(sql)
34+
data = cur.fetchall()
35+
cur.close()
36+
conn.close()
37+
return data
38+
39+
def executeUpdate(self, sql):
40+
conn = self.getConnection()
41+
cur = conn.cursor()
2742
cur.execute(sql)
28-
sqlData = cur.fetchall()
43+
conn.commit()
2944
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()
45+
conn.close()
46+
47+
def executeQuery(self, sql):
48+
conn = self.getConnection()
49+
cur = conn.cursor()
4150
cur.execute(sql)
51+
data = cur.fetchall()
4252
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)
53+
conn.close()
54+
return data
55+
56+
def close(self):
57+
if self.pool is not None:
58+
self.pool.close()
59+
self.pool = None

0 commit comments

Comments
 (0)