MySQL 使用连接池封装pymysql
备注:1,记得先修改连接的数据库哦,(用navicat更方便一点);2,分开两个py文件写入,运行sqlhelper.py文件
一、在utils.py中写
import pymysql
from DBUtils.PooledDB import PooledDB POOL = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
mincached=2, # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
maxcached=5, # 链接池中最多闲置的链接,0和None不限制
maxshared=1, # 链接池中最多共享的链接数量,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='ziji',
charset='utf8'
) 二、在sqlhelper.py中写入:
from utils import POOL
import pymysql def create_conn():
conn = POOL.connection()
cursor = conn.cursor(pymysql.cursors.DictCursor)
return conn, cursor def close_conn(conn, cursor):
conn.close()
cursor.close() def select_one(sql, args):
conn, cur = create_conn()
cur.execute(sql, args)
result = cur.fetchone()
close_conn(conn, cur)
return result def select_all(sql, args):
conn, cur = create_conn()
cur.execute(sql, args)
result = cur.fetchall()
close_conn(conn, cur)
return result def insert_one(sql, args):
conn, cur = create_conn()
result = cur.execute(sql, args)
conn.commit()
close_conn(conn, cur)
return result def delete_one(sql,args):
conn,cur = create_conn()
result = cur.execute(sql,args)
conn.commit()
close_conn(conn,cur)
return result def update_one(sql,args):
conn,cur = create_conn()
result = cur.execute(sql,args)
conn.commit()
close_conn(conn,cur)
return result # sql = "insert into stu(id,name,age) VALUE (%s,%s,%s)" #增加
# res = insert_one(sql, [2,"飞机", 28])
# print(res) # sql = "delete from stu where name = %s" #删除
# res = delete_one(sql, "哪吒")
# print(res) # sql = "select * from stu" #查询全部
# res = select_all(sql, [])
# print(res) # sql = "select * from stu where name=%s" #查询一条
# res = select_one(sql, "赵振伟")
# print(res) # sql = " UPDATE stu set name=%s where name = %s" #更新
# res = update_one(sql,("坦克","飞机"))
# print(res) 转载自:https://www.cnblogs.com/zzw731862651/articles/9605308.html
MySQL 使用连接池封装pymysql的更多相关文章
- Node.js使用MySQL的连接池
使用Nodejs+MySQL肯定比PHP和MySQL的组合更适合做服务器端的开发. 使用Nodejs你会从他的异步行为中获益良多.比如,提升性能,你无须在从已有的MySQL数据库迁移到其他的NoSQL ...
- [nodejs]解决mysql和连接池(pool)自动断开问题
最近在做一个个人项目,数据库尝试使用了mongodb.sqlite和mysql.分享一下关于mysql的连接池用法.项目部署于appfog,项目中我使用连接池链接数据库,本地测试一切正常.上线以后,经 ...
- Python下Mysql数据连接池——单例
# coding:utf-8 import threading import pymysql from DBUtils.PooledDB import PooledDB from app.common ...
- nodejs mysql 创建连接池
用Nodejs连接MySQL 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javas ...
- MySQL&ES连接池
数据库的连接池建议放在类似settings.py的配置模块中,因为基本都是配置项,方便统一管理. 1) 连接池类#settings.py import os from DBUtils.PooledDB ...
- hibernate+mysql的连接池配置
1:连接池的必知概念 首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...
- Tomcat 下 mysql的连接池配置和使用
最近维护的一个项目出了问题,最后分析是卡在数据库连接池上,然后就做了些学习. 先把我自己的方法写出来,再说下网上其他的没有成功的方法. 1.首先当然是先把mysql的jar包放在lib目录下,tonc ...
- c3p0连接池封装
在处理数据库事物时需要同一个Connection 但是dbcp无法获得 单独工具也显得繁琐,改进成c3p0工具类: package utils; import java.sql.Connectio ...
- 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)
1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...
随机推荐
- MongoDB部分
- tomcat端口号被占用问题
1 netstat -ano| findstr 8761 2 taskkill /f/t/im 5156
- 怎样理解window.name
window.name表示当前窗口的名字, 而非网页的名字, 网页的名字需要使用: document.title; window.name一般是空的字符串, 他的作用其实是配合配合超链接和表单的tar ...
- jQuery.print.js
登录网址https://github.com/DoersGuild/jQuery.print,下载js文件,进行简单的配置即可使用啦! 配置参数你可以在调用打印方法时传入一些参数: $("# ...
- UnityC#中修改RectTransform
1.改变RectTransform的Left和Buttom GetComponent<RectTransform>().offsetMax = new Vector2(left, top) ...
- Ubuntu 14.04 用户如何安装深度音乐播放器和百度音乐插件
播放本地音乐或者收听国外的音乐电台,Ubuntu 14.04 自带的音乐播放器 Rhythmbox 完全能够满足,但是如果你想有像酷狗那样的国内播放器就需要折腾一下,还好有深度音乐播放器,这是一款完全 ...
- findstr 命令使用
findstr 命令使用 find /? 在文件中搜索字符串. FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][p ...
- Tensorflow 训练inceptionV4 并移植
安装brazel (请使用最新版的brazel 和最新版的tensorflow ,版本不匹配会出错!!!) 下载bazel-0.23 https://pan.baidu.com/s/1X ...
- vmware 虚拟机扩展 liunx系统硬盘空间
参考一下以下博客 https://www.cnblogs.com/yongdaimi/p/9050155.html https://blog.csdn.net/daemon_2017/article/ ...
- 如何让某些用户对Marketing Cloud的contact数据只能实施只读操作
打开maintain business role这个应用: 创建一个新的business role,然后添加下列这几个catalogs: SAP_CEC_BC_MKT_ADM_PC Marketing ...