- python数据库连接池
- 如果每一个请求过来就创建一个链接这样效率会很低,所以需要建立连接池
- DBUtils是python的一个用于实现数据库连接池的模块
# pip3 安装 pip3 install DBUtils
- 创建数据库连接池
- 为每个线程创建一个连接,线程即使调用了close方法,也不会关闭,只是把连接重新放到连接池,供自己线程再次使用。当线程终止时,连接自动关闭
import pymysql from DBUtils.PooledDB import PooledDB POOL = PooledDB( creator=pymysql, # 使用链接数据库的模块 maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数 mincached=2, # 初始化时,链接池中至少创建的空闲的链接,0表示不创建 maxcached=5, # 链接池中最多闲置的链接,0和None不限制 maxshared=3, # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。 blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错 maxusage=None, # 一个链接最多被重复使用的次数,None表示无限制 setsession=[], # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."] ping=0, # ping MySQL服务端,检查是否服务可用。 # 如:0 = None = never, # 1 = default = whenever it is requested, # 2 = when a cursor is created, # 4 = when a query is executed, # 7 = always # 下面为mysql连接配置 host="127.0.0.1", port=3306, user="root", password="", charset="utf8", db="userinfo" )
pymysql基础操作点我:https://www.cnblogs.com/lzmdbk/p/10269042.html
- 简单查询
- 注意检测当前正在运行连接数的是否小于最大链接数,如果不小于则:等待或报raise TooManyConnections异常
conn = POOL.connection() cur = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "select * from users WHERE name=‘小白‘ and age=16 " res = cur.execute(sql) print(cur.fetchall()) conn.close()
结果:
[{‘id‘: 3, ‘name‘: ‘小白‘, ‘age‘: 16}]
- 自主增删改查工具
dbpool.py
import pymysql from DBUtils.PooledDB import PooledDB POOL = PooledDB( creator=pymysql, maxconnections=6, mincached=2, maxcached=5, maxshared=3, blocking=True, maxusage=None, setsession=[], ping=0, host="127.0.0.1", port=3306, user="root", password="", charset="utf8", db="userinfo" )
dbpool.py
sqlhelper.py
from dbpool import POOL import pymysql def create_conn(): # 创建连接 conn = POOL.connection() cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) return conn,cursor def close_conn(conn,cursor): # 关闭连接 cursor.close() conn.close() def fetch_all(sql,args=None): # 查询全部 conn,cursor = create_conn() cursor.execute(sql,args) res = cursor.fetchall() close_conn(conn,cursor) return res # sql = "select * from users" # # print(fetch_all(sql)) # [{‘id‘: 1, ‘name‘: ‘小明‘, ‘age‘: 18}, {‘id‘: 2, ‘name‘: ‘小黑‘, ‘age‘: 17}, {‘id‘: 3, ‘name‘: ‘小白‘, ‘age‘: 16}] def fetch_one(sql,args): # 单个查询 conn,cursor = create_conn() cursor.execute(sql,args) res = cursor.fetchone() close_conn(conn,cursor) return res # sql = "select * from users where name=%s and age=%s" # # print(fetch_one(sql,("小明",18))) # {‘id‘: 1, ‘name‘: ‘小明‘, ‘age‘: 18} def insert(sql,args): conn,cursor = create_conn() res = cursor.execute(sql,args) conn.commit() close_conn(conn, cursor) return res # sql = "insert into users(id,name,age) value (%s,%s,%s)" # # print(insert(sql,(4,"小王",20))) def delete(sql,args): conn, cursor = create_conn() res = cursor.execute(sql, args) conn.commit() close_conn(conn, cursor) return res sql = "delete from users where id = %s" print(delete(sql,4))
sqlhelper.py
原文地址:https://www.cnblogs.com/lzmdbk/p/10269112.html
时间: 2024-11-05 18:48:04