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 ...
随机推荐
- golang字符串常用的系统函数
1.统计字符串的长度,按字节len(str) str := "hello北京" fmt.Println("str len=", len(str)) 2.字符串遍 ...
- pandas中的argsort
直接通过例子看比较好理解. import pandas as pd data = [[1, 2, 3], [2, 2, 2], [7, 8, 9]] df = pd.DataFrame(data, i ...
- linux上启动tomcat报错:Failed to read schema document 'http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd
本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! spring在加载xsd文件时总是先试图在本地查找xs ...
- axios的二次封装
'use strict' import axios from 'axios' import qs from 'qs' var host = "https://www.easy-mock.co ...
- JDBC 复习1 DBUtil
package dbex; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; impo ...
- SIFT算法研究
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://underthehood.blog.51cto.com/2531780/65835 ...
- 一、eureka服务端自动配置
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 @EnableEurekaServer开关 eureka是一个c/s架构的服务治理框架, ...
- form表单详解
form表单 form是一个复杂的系统标签,其内部又可包含很多的一些输入标签 例如input 输入文本标签 checkbox 多选标签等等 form表单有几个属性我们需要注意一下 1:action属 ...
- 【leetcode】610. Triangle Judgement
原题 A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. ...
- 深度学习_1_神经网络_4_分布式Tensorflow
分布式Tensorflow 单机多卡(gpu) 多级多卡(分布式) 自实现分布式 API: 1,创建一个tf.train.ClusterSpec,用于对集群的所有任务进行描述,该描述对于所有任务相 ...