安装包
pip install DBUtils
pip install cx_Oracle
pip install MySQLdb
DBUtils下载地址:https://pypi.python.org/pypi/DBUtils/
oracle 连接池方式:
from DBUtils.PooledDB import PooledDB
import cx_Oracle
user='XXXXX'
password='XXXXXXXXX'
host='XXXXXXXXXXXXXXX'
port='XXXX'
sid='XXX'
dsn=cx_Oracle.makedsn(host,port,sid)
pool = PooledDB(cx_Oracle,mincached=20,blocking=True,user=user,password=password,dsn=dsn)
conn = pool.connection()
cur=conn.cursor()
SQL="此次写查询sql语句"
r=cur.execute(SQL)
r=cur.fetchall()
print(r)
cur.close()
conn.close()
mysql 连接池方式:
import MySQLdb
from DBUtils.PooledDB import PooledDB
pool = PooledDB(MySQLdb,5,host='localhost',user='root',passwd='pwd',db='myDB',port=3306) #5为连接池里的最少连接数
conn = pool.connection() #以后每次需要数据库连接就是用connection()函数获取连接就好了
cur=conn.cursor()
SQL="select * from table1"
r=cur.execute(SQL)
r=cur.fetchall()
cur.close()
conn.close()