Python数据库连接池DBUtils.PooledDB,,python不用连接池的


python不用连接池的MySQL连接方法

import MySQLdbconn= 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 MySQLdbfrom DBUtils.PooledDB import PooledDBpool = 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/

评论关闭