数据库连接池
| |
| |
| |
| |
| import pymysql |
| from DBUtils.PooledDB import PooledDB |
| |
| POOL = PooledDB( |
| creator=pymysql, |
| maxconnections=6, |
| mincached=2, |
| |
| |
| blocking=True, |
| |
| |
| |
| host='127.0.0.1', |
| port=3306, |
| user='root', |
| password='p@ss0rd', |
| |
| database='cov', |
| charset='utf8' |
| ) |
| |
| |
| conn = POOL.connection() |
| |
| cursor = conn.cursor() |
| cursor.execute('select * from drug') |
| result = cursor.fetchall() |
| |
| cursor.close() |
| |
| |
| conn.close() |
| |
| print(result) |
多线程测试
| |
| |
| |
| |
| import pymysql |
| from threading import Thread |
| from DBUtils.PooledDB import PooledDB |
| |
| POOL = PooledDB( |
| creator=pymysql, |
| maxconnections=6, |
| mincached=2, |
| |
| |
| blocking=True, |
| |
| |
| |
| host='127.0.0.1', |
| port=3306, |
| user='root', |
| password='p@ss0rd', |
| |
| database='cov', |
| charset='utf8' |
| ) |
| |
| |
| |
| |
| def task(num): |
| |
| conn = POOL.connection() |
| |
| cursor = conn.cursor() |
| |
| cursor.execute('select sleep(3)') |
| result = cursor.fetchall() |
| |
| cursor.close() |
| |
| |
| conn.close() |
| print(num, "-------------->", result) |
| |
| |
| |
| for i in range(57): |
| t = Thread(target=task, args=(i,)) |
| t.start() |
函数实现sqlhelper
| |
| |
| |
| |
| import pymysql |
| from DBUtils.PooledDB import PooledDB |
| |
| POOL = PooledDB( |
| creator=pymysql, |
| maxconnections=6, |
| mincached=2, |
| |
| |
| blocking=True, |
| |
| |
| |
| host='127.0.0.1', |
| port=3306, |
| user='root', |
| password='p@ss0rd', |
| |
| database='cov', |
| charset='utf8' |
| ) |
| |
| def fetchall(sql, *args): |
| """获取所有数据""" |
| |
| conn = POOL.connection() |
| cursor = conn.cursor() |
| cursor.execute(sql, args) |
| result = cursor.fetchall() |
| cursor.close() |
| conn.close() |
| |
| return result |
| |
| def fetchone(sql, *args): |
| """获取单条数据""" |
| |
| conn = POOL.connection() |
| cursor = conn.cursor() |
| cursor.execute(sql, args) |
| result = cursor.fetchone() |
| cursor.close() |
| conn.close() |
| |
| return result |
单例模式
| |
| |
| |
| |
| import pymysql |
| from DBUtils.PooledDB import PooledDB |
| |
| class SqlHelper: |
| def __init__(self): |
| self.pool = PooledDB( |
| creator=pymysql, |
| maxconnections=6, |
| mincached=2, |
| blocking=True, |
| host='127.0.0.1', |
| port=3306, |
| user='root', |
| password='p@ss0rd', |
| |
| database='cov', |
| charset='utf8' |
| ) |
| |
| |
| def __open(self): |
| conn = self.pool.connection() |
| cursor = conn.cursor() |
| return conn, cursor |
| |
| def __close(self, cursor, conn): |
| cursor.close() |
| conn.close() |
| |
| |
| def fetchall(self, sql, *args): |
| """获取所有数据""" |
| |
| conn, cursor = self.__open() |
| cursor.execute(sql, args) |
| result = cursor.fetchall() |
| self.__close(cursor, conn) |
| |
| return result |
| |
| def fetchone(self, sql, *args): |
| """获取单条数据""" |
| |
| conn, cursor = self.__open() |
| cursor.execute(sql, args) |
| result = cursor.fetchone() |
| self.__close(cursor, conn) |
| |
| return result |
| |
| |
| db = SqlHelper() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步