甩祸 发表于 2015-4-28 07:13:27

Python数据库连接池实例——PooledDB

  不用连接池的MySQL连接方法



import MySQLdb
conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306)
cur=conn.cursor()
SQL="select * from table1"
r=cur.execute(SQL)
r=cur.fetchall()
cur.close()
conn.close()
  用连接池后的连接方法



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()
  DBUtils下载地址:https://pypi.python.org/pypi/DBUtils/
页: [1]
查看完整版本: Python数据库连接池实例——PooledDB