flask中是没有ORM的,如果在flask里面连接数据库有两种方式
一:pymysql
二:SQLAlchemy
是python 操作数据库的一个库。能够进行 orm 映射官方文档 sqlchemy
SQLAlchemy“采用简单的Python语言,为高效和高性能的数据库访问设计,实现了完整的企业级持久模型”。SQLAlchemy的理念是,SQL数据库的量级和性能重要于对象集合;而对象集合的抽象又重要于表和行。
链接池原理
- BDUtils数据库链接池
- 模式一:基于threaing.local实现为每一个线程创建一个连接,关闭是
伪关闭,当前线程可以重复
- 模式二:连接池原理
- 可以设置连接池中最大连接数 9
- 默认启动时,连接池中创建连接 5
- 如果有三个线程来数据库中获取连接:
- 如果三个同时来的,一人给一个链接
- 如果一个一个来,有时间间隔,用一个链接就可以为三个线程提供服务
- 说不准
有可能:1个链接就可以为三个线程提供服务
有可能:2个链接就可以为三个线程提供服务
有可能:3个链接就可以为三个线程提供服务
PS、:maxshared在使用pymysql中均无用。链接数据库的模块:只有threadsafety>1的时候才有用
那么我们用pymysql来做。
为什么要使用数据库连接池呢?不用连接池有什么不好的地方呢?
方式一
缺点 : 每次操作都要链接数据库,链接次数过多
#!usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
from flask import Flask
app = Flask(__name__)
# 方式一:这种方式每次请求,反复创建数据库链接,多次链接数据库会非常耗时
# 解决办法:放在全局,单例模式
@app.route('/index')
def index():
# 链接数据库
conn = pymysql.connect(host="127.0.0.1",port=3306,user='root',password='123', database='pooldb',charset='utf8')
cursor = conn.cursor()
cursor.execute("select * from td where id=%s", [5, ])
result = cursor.fetchall() # 获取数据
cursor.close()
conn.close() # 关闭链接
print(result)
return "执行成功"
if __name__ == '__main__':
app.run(debug=True)
方式二
不支持并发
#!usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
from flask import Flask
from threading import RLock
app = Flask(__name__)
CONN = pymysql.connect(host="127.0.0.1",port=3306,user='root',password='123', database='pooldb',charset='utf8')
# 方式二:放在全局,如果是单线程,这样就可以,但是如果是多线程,就得加把锁。这样就成串行的了
# 不支持并发,也不好。所有我们选择用数据库连接池
@app.route('/index')
def index():
with RLock:
cursor = CONN.cursor()
cursor.execute("select * from td where id=%s", [5, ])
result = cursor.fetchall() # 获取数据
cursor.close()
print(result)
return "执行成功"
if __name__ == '__main__':
app.run(debug=True)
方式三 :
由于上面两种方案都不完美,所以得把方式一和方式二联合一下(既让减少链接次数,也能支持并发)所有了方式三,需要
导入一个DButils模块
基于DButils实现的数据库连接池有两种模式:
模式一:
为每一个线程创建一个链接(是基于本地线程来实现的。thread.local),每个线程独立使用自己的数据库链接,该线程关闭不是真正的关闭,本线程再次调用时,还是使用的最开始创建的链接,直到线程终止,数据库链接才关闭
注: 模式一:如果线程比较多还是会创建很多连接,模式二更常用
#!usr/bin/env python
# -*- coding:utf-8 -*-
from DBUtils.PersistentDB import PersistentDB
import pymysql
from flask import Flask
app = Flask(__name__)
POOL = PersistentDB(
creator=pymysql, # 使用链接数据库的模块
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
closeable=False,
# 如果为False时, conn.close() 实际上被忽略,供下次使用,再线程关闭时,才会自动关闭链接。如果为True时, conn.close()则关闭链接,那么再次调用pool.connection时就会报错,因为已经真的关闭了连接(pool.steady_connection()可以获取一个新的链接)
threadlocal=None, # 本线程独享值得对象,用于保存链接对象,如果链接对象被重置
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='pooldb',
charset='utf8'
)
@app.route('/func')
def func():
conn = POOL.connection()
cursor = conn.cursor()
cursor.execute('select * from tb1')
result = cursor.fetchall()
cursor.close()
conn.close() # 不是真的关闭,而是假的关闭。 conn = pymysql.connect() conn.close()
conn = POOL.connection()
cursor = conn.cursor()
cursor.execute('select * from tb1')
result = cursor.fetchall()
cursor.close()
conn.close()
if __name__ == '__main__': app.run(debug=True)
模式二:
创建一个链接池,为所有线程提供连接,使用时来进行获取,使用完毕后在放回到连接池。
PS:假设最大链接数有10个,其实也就是一个列表,当你pop一个,人家会在append一个,链接池的所有的链接都是按照排队的这样的方式来链接的。
链接池里所有的链接都能重复使用,共享的, 即实现了并发,又防止了链接次数太多
import time
import pymysql
import threading
from DBUtils.PooledDB import PooledDB, SharedDBConnection
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
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='pooldb',
charset='utf8'
)
def func():
# 检测当前正在运行连接数的是否小于最大链接数,如果不小于则:等待或报raise TooManyConnections异常
# 否则
# 则优先去初始化时创建的链接中获取链接 SteadyDBConnection。
# 然后将SteadyDBConnection对象封装到PooledDedicatedDBConnection中并返回。
# 如果最开始创建的链接没有链接,则去创建一个SteadyDBConnection对象,再封装到PooledDedicatedDBConnection中并返回。
# 一旦关闭链接后,连接就返回到连接池让后续线程继续使用。
# PooledDedicatedDBConnection
conn = POOL.connection()
# print(th, '链接被拿走了', conn1._con)
# print(th, '池子里目前有', pool._idle_cache, '\r\n')
cursor = conn.cursor()
cursor.execute('select * from tb1')
result = cursor.fetchall()
conn.close()
conn = POOL.connection()
# print(th, '链接被拿走了', conn1._con)
# print(th, '池子里目前有', pool._idle_cache, '\r\n')
cursor = conn.cursor()
cursor.execute('select * from tb1')
result = cursor.fetchall()
conn.close()
func()
自制sqlhelper
1 class MySQLhelper(object):
2 def __init__(self, host, port, dbuser, password, database):
3 self.pool = PooledDB(
4 creator=pymysql, # 使用链接数据库的模块
5 maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
6 mincached=2, # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
7 maxcached=5, # 链接池中最多闲置的链接,0和None不限制
8 maxshared=3,
9 # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
10 blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
11 maxusage=None, # 一个链接最多被重复使用的次数,None表示无限制
12 setsession=[], # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
13 ping=0,
14 # 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
15 host=host,
16 port=int(port),
17 user=dbuser,
18 password=password,
19 database=database,
20 charset='utf8'
21 )
22
23 def create_conn_cursor(self):
24 conn = self.pool.connection()
25 cursor = conn.cursor(pymysql.cursors.DictCursor)
26 return conn,cursor
27
28 def fetch_all(self, sql, args):
29 conn,cursor = self.create_conn_cursor()
30 cursor.execute(sql,args)
31 result = cursor.fetchall()
32 cursor.close()
33 conn.close()
34 return result
35
36
37 def insert_one(self,sql,args):
38 conn,cursor = self.create_conn_cursor()
39 res = cursor.execute(sql,args)
40 conn.commit()
41 print(res)
42 conn.close()
43 return res
44
45 def update(self,sql,args):
46 conn,cursor = self.create_conn_cursor()
47 res = cursor.execute(sql,args)
48 conn.commit()
49 print(res)
50 conn.close()
51 return res
52
53
54 sqlhelper = MySQLhelper("127.0.0.1", 3306, "root", "1233121234567", "dragon")
55
56 # sqlhelper.fetch_all("select * from user where id=%s",(1))
57
58 # sqlhelper.insert_one("insert into user VALUES (%s,%s)",("jinwangba",4))
59
60 # sqlhelper.update("update user SET name=%s WHERE id=%s",("yinwangba",1))