1. 未使用连接池连接mysql
import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1', user ='user1', passwd = 'your-password', db ='db1', port = 3306)
cur = conn.cursor()
SQL ="select * from t1"
r = cur.execute(SQL)
r = cur.fetchall()
cur.close()
conn.close()
2. 使用连接池连接mysql
import MySQLdb
from DBUtils.PooledDB import PooledDB
pool = PooledDB(MySQLdb, 5, host = '127.0.0.1', user = 'user1', passwd = 'your-password', db = 'db1', port = 3306)
conn = pool.connection()
cur = conn.cursor()
SQL = "select * from t1"
r = cur.execute(SQL)
r = cur.fetchall()
cur.close()
conn.close()
1. 未使用连接池连接mysqlimport MySQLdbconn = MySQLdb.connect(host = '127.0.0.1', user ='user1', passwd = 'your-password', db ='db1', port = 3306) cur = conn.cursor()SQL ="select * from t1"r = cur.execute(
import pymysql, os, configparser
from pymysql.cursors import DictCursor
from
DBUtils
.
PooledDB
import
PooledDB
class Config(object):
# Config().get_content("user_information")
配置文件里面...
因为Anaconda中没有
DBUtils
包,所以pip命令安装的
DBUtils
是假的,在pycharm 的project interpreter中是没有
dbutils
的。
Anaconda正确安装
DButils
姿势:
1.anaconda search ...
您好!使用 pymysql
连接
数据库
连接
池
的方法如下:
1. 安装
数据库
连接
池
库。在使用 pymysql
连接
数据库
连接
池
时,需要使用另一个库来管理
连接
池
,比如
DBUtils
。你可以使用 `pip install
DBUtils
` 来安装这个库。
2. 导入所需的模块。在你的代码中,你需要导入 pymysql、
DBUtils
和
DBUtils
.
PooledDB
模块。
```
python
import pymysql
from
DBUtils
.
PooledDB
import
PooledDB
3. 创建
连接
池
。使用
DBUtils
.
PooledDB
模块中的
PooledDB
类创建
连接
池
。
```
python
# 创建
连接
池
pool =
PooledDB
(
creator=pymysql, # 使用的
数据库
模块
maxconnections=10, #
连接
池
最大
连接
数
mincached=2, # 初始化时
连接
池
至少创建的空闲
连接
maxcached=5, #
连接
池
最多空闲
连接
数
maxshared=3, #
连接
池
最多共享
连接
数
blocking=True, #
连接
数达到最大是否阻塞
maxusage=None, # 一个
连接
最多被重复使用的次数
setsession=[], #
连接
池
自动重置的SQL命令
ping=0,
# ping MySQL服务端,检查是否服务可用
host='127.0.0.1',
port=3306,
user='username',
password='password',
database='database',
charset='utf8'
4. 从
连接
池
获取
连接
。使用
连接
池