django中有强大的ORM支持我们来操作数据库, 但是flask没有提供对数据库的操作, 依然还是需要第三方的支持, 来提高我们的开发效率.

下载DBUtils

使用DBUtils

使用DBUtils只需要实例化, 就会产生一个数据库的连接池, 但是实例化过程中会有一些参数来定义连接池

DBUtils参数解释

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='',
database='pooldb',
charset='utf8'
)

我创建的连接池

import pymysql
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='192.168.233.128',
port=3306,
user='root',
password='',
database='superCRM',
charset='utf8'
)

升级一下我的连接池, 类似于ORM

class SathQuery(object):
_POOL = None def __new__(cls, *args, **kwargs):
if not cls._POOL:
cls._POOL = super().__new__(cls)
return cls._POOL def __init__(self, user, password, database, charset, host="127.0.0.1", post=3306):
self._POOL = PooledDB(
creator=pymysql,
maxconnections=6,
mincached=2,
maxcached=5,
maxshared=3,
blocking=True,
maxusage=None,
setsession=[],
ping=0,
host=host,
port=post,
user=user,
password=password,
database=database,
charset=charset
) def get_connection(self):
"""
获取连接
:return:
"""
conn = self._POOL.connection()
cursor = conn.cursor(pymysql.cursors.DictCursor)
return conn, cursor def fetch_all(self, sql, args):
"""
查询所有
:param sql:
:param args:
:return:
"""
conn, cursor = self.get_connection()
cursor.execute(sql, args)
result = cursor.fetchall()
cursor.close()
conn.close()
return result def insert_one(self, sql, args):
"""
查询一个
:param sql:
:param args:
:return:
"""
conn, cursor = self.get_connection()
res = cursor.execute(sql, args)
conn.commit()
print(res)
conn.close()
return res def update(self, sql, args):
"""
更新数据
:param sql:
:param args:
:return:
"""
conn, cursor = self.get_connection()
res = cursor.execute(sql, args)
conn.commit()
print(res)
conn.close()
return res

使用高级一点的数据库连接池进行数据库操作

# 创建连接池
query = SathQuery("root", "", "superCRM", "utf8", host="192.168.233.128") # 进行数据库操作
print(query.fetch_all("select * from superCRM_customer where id=%s", (4,))) print(query.insert_one("insert into superCRM_customer VALUES (%s,%s,%s)", (7, "sath", ""))) print(query.update("update user SET name=%s WHERE id=%s", ("wang", 1)))

flask第三方插件DBUtils的更多相关文章

  1. flask第三方插件WTForms

    在django中有ModelForm, 虽然flask原生没有提供, 但是强大的第三方也提供了这样的功能 虽然不如django的强大, 但是基本的功能还是可以有的, 下面就来使用一哈. WTForms ...

  2. Flask框架 (四)—— 请求上下文源码分析、g对象、第三方插件(flask_session、flask_script、wtforms)、信号

    Flask框架 (四)—— 请求上下文源码分析.g对象.第三方插件(flask_session.flask_script.wtforms).信号 目录 请求上下文源码分析.g对象.第三方插件(flas ...

  3. 4.flask第三方组件

    1.flask-session的使用 在flask中,有一个app.session_interface = SecureCookieSessionInterface(),也就是存session,调用o ...

  4. zabbix通过第三方插件percona监控mysql数据库

     zabbix通过第三方插件percona监控mysql数据库                                                                     ...

  5. iOS 开发:利用第三方插件来安装CoCoapods

    引言:通过上一篇博客我们知道了怎么样去通过终端来安装CoCoapods,这一篇我们着重与用第三方插件来安装CoCoapods: 1. 首先在提下链接下载插件 https://github.com/ka ...

  6. 苹果下如果安装nginx,给nginx安装markdown第三方插件

    用brew install nginx 这样安装的是最新版的nginx, 但是在有些情况下,安装第三方插件需要特定的版本,更高一级的版本可能装不上. 它的原理是下载安装包进行自动安装,建立软链,这样就 ...

  7. ThinkPHP自动获取关键词(调用第三方插件)

    ThinkPHP自动获取关键词调用在线discuz词库 先按照下图路径放好插件 方法如下 /** * 自动获取关键词(调用第三方插件) * @return [type] [description] * ...

  8. iOS 学习笔记 十 (2015.04.03)xcode第三方插件

    1.xcode第三方插件,存放路径:~/Library/Application Support/Developer/Shared/Xcode/Plug-ins

  9. zatree第三方插件

    Zabbix安装第三方插件zatree2.4.5 1.下载zatree第三方插件https://github.com/spide4k/zatree.git 2.检查PHP环境需要支持php-xml.p ...

随机推荐

  1. webStrorm 简单配置

    1.主题配色 主题设置 File -> Settings -> Appearance & Behavior -> Appearance ->Theme.    ===& ...

  2. JAVA程序员常用开发工具

    1.JDK (Java Development Kit)Java开发工具集 SUN的Java不仅提了一个丰富的语言和运行环境,而且还提了一个免费的Java开发工具集(JDK).开发人员和最终用户可以利 ...

  3. 【Leetcode】【Medium】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  4. php 汉字转拼音函数

    function Pinyin($_String, $_Code='UTF8'){ //GBK页面可改为gb2312,其他随意填写为UTF8 $_DataKey = "a|ai|an|ang ...

  5. Nginx配置虚拟机,url重写,防盗链

    配置目录: ·     虚拟主机 ·     PHP支持 ·     URL重写 ·     防止盗链 ·     持续更新… 一.虚拟主机 1.创建 文件格式:{域名}.conf 具体如下: $ s ...

  6. 第三周 day3 python学习笔记

    1.字符串str类型,不支持修改. 2.关于集合的学习: (1)将列表转成集合set:集合(set)是无序的,集合中不会出现重复元素--互不相同 (2)集合的操作:交集,并集.差集.对称差集.父集.子 ...

  7. 如何在 MSBuild Target(Exec)中报告编译错误和编译警告

    编译错误和编译警告 MSBuild 的 Exec 自带有错误和警告的标准格式,按照此格式输出,将被识别为编译错误和编译警告. 而格式只是简简单单的 error: 开头或者 warning: 开头.冒号 ...

  8. Ubuntu 下安装apache+PHP

    1.安装apache2 sudo apt-get install apache2 运行如下命令重启:sudo /etc/init.d/apache2 restart 在浏览器里输入http://loc ...

  9. python UI自动化实战记录八:添加配置

    添加配置文件写入测试地址等,当环境切换时只需修改配置文件即可. 1 在项目目录下添加文件 config.ini 写入: [Domain] domain = http://test.domain.cn ...

  10. 智能机器人“小昆”的实现(五)MainActivty的实现及项目结束

    好了,一切准备工作都完成了,下面我们就可以真正的编写MainActivity了.在MainActivity中,我们要为ListView设定适配器,并为发送按钮设定点击事件.我们的逻辑就是点击发送按钮, ...