Database connection pool
Why would you use the database connection pool
- Frequent connection and disconnection database, consumption, low efficiency
- DBUtils can create multiple threads connect to the database, and always connected, not disconnected
- When performing database operations, the data pool allocating threads, when the data pool empty, waiting for selectively polishing or wrong
installation
pip3 install DBUtils
Basic usage
Create a data pool
`` `Python
Import Time
Import Threadingimport pymysql
from DBUtils.PooledDB import PooledDB# Create a database connection pool
the POOL = PooledDB (
Creator = pymysql,
MaxConnections = 20 is, # define the maximum number of connections
mincached 2, # define the starting number of connections =
Host '127.0.0.1', =
blocking = True, used up connection pool # , True is waiting, False when throwing error
Port = 3306,
User = 'the root',
password = '123',
Database = 'poolDB',
charset = 'UTF8'
)# Database connection pool to obtain a connection
conn = POOL.connection ()cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute('select * from tb1')
result = cursor.fetchall()# Put this connection back to the connection pool
conn.Close ()
`` `
Application in the flask
The definition of
`` `
Import pymysqlfrom DBUtils.PooledDB import PooledDB
SQLHelper class (Object):
DEF the init (Self):
# Create a database connection pool
self.pool = PooledDB (
Creator = pymysql,
MaxConnections =. 5,
mincached = 2,
blocking = True,
Host = '127.0.0.1',
Port = 3306 ,
User = 'the root',
password = '123',
Database = 's23day02',
charset = 'UTF8'
)def connect(self): conn = self.pool.connection() cursor = conn.cursor() return conn,cursor def disconnect(self,conn,cursor): cursor.close() conn.close() def fetchone(self,sql,params=None): """ 获取单条 :param sql: :param params: :return: """ if not params: params = [] conn,cursor = self.connect() cursor.execute(sql, params) result = cursor.fetchone() self.disconnect(conn,cursor) return result def fetchall(self,sql,params=None): """ 获取所有 :param sql: :param params: :return: """ import pymysql if not params: params = [] conn, cursor = self.connect() cursor.execute(sql,params) result = cursor.fetchall() self.disconnect(conn, cursor) return result def commit(self,sql,params): """ 增删改 :param sql: :param params: :return: """ import pymysql if not params: params = [] conn, cursor = self.connect() cursor.execute(sql, params) conn.commit() self.disconnect(conn, cursor)
db = SQLHelper()
```Example single mode
from flask import Blueprint,url_for,request,render_template,session,redirect from ..utils.sqlhelper import db # 创建了一个蓝图对象 account = Blueprint('account',__name__) @account.route('/login',methods=['GET','POST']) def login(): if request.method == 'GET': return render_template('login.html') user = request.form.get('user') pwd = request.form.get('pwd') # 根据用户名和密码去数据库进行校验 # 连接/SQL语句/关闭 result = db.fetchone('select * from user where username=%s and password=%s',[user,pwd]) if result: # 在session中存储一个值 session['user_info'] = user return redirect(url_for('user.user_list')) return render_template('login.html',error="用户名或密码错误")
[Description] SQLhelper class
- Related methods package database operations, so that after the transfer of business functions, reducing code when a certain degree of repetition
- With DBUtils, greatly improving the operating efficiency of the database