添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

# -*- coding: utf-8 -*-
import MySQLdb
from DBUtils.PooledDB import PooledDB
#5为连接池里的最少连接数
pool = PooledDB(MySQLdb, 5, host = '127.0.0.1', user = 'user1', passwd = 'your-password', db = 'db1', port = 3306) 
#后续每次需要数据库连接用connection()函数获取连接即可。
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. 从 连接 获取 连接 。使用 连接