Python mysql连接池 01

查询语句

import pymysql
# from g_conf.config import config_template
from DBUtils.PooledDB import PooledDB

# 打开一个链接,20是最大连接数,pymysql是上面导入的模块名,后面的顾名思义
pool = PooledDB(pymysql,20,host='10.0.0.4',database='excel_data',port=3306,user='root',password='Sinobase@123')
# 打开一个链接
conn = pool.connection()
cur = conn.cursor()
# 执行SQL
sql = 'insert into user values("2","刘老六","666")'
cur.execute(sql)
# 获取返回值
re = cur.fetchall()
print(re)
# 关闭当前连接,提交sql
cur.close()
conn.commit()
conn.close()

select:

import pymysql
# from g_conf.config import config_template
from DBUtils.PooledDB import PooledDB

pool = PooledDB(pymysql,20,host='10.0.0.4',database='excel_data',port=3306,user='root',password='Sinobase@123')
conn = pool.connection()
cur = conn.cursor()
sql = 'select * from user;'
cur.execute(sql)
re = cur.fetchall()
print(re)

cur.close()
conn.close()

你可能感兴趣的:(python基础)