通过以下的内容你就可以轻松的运用Python数据库连接池的相关步骤,希望下面的文章会对你有所收获。
请求连接:
1.db=pool.connection()
2.你可以使用这些连接有如原始的DB-API2一样。而实际使用的是``SteadyDB``版本的强硬连接。请注意连接可以与其他线程共享,只要你设置maxshared参数为非零,并且DB-API2模块也允许。
如果你想要使用专用连接则使用:
1.db=pool.connection(0)
2.如果你不再需要这个连接了,则可以返回给连接池使用db.close()。
你也可以使用相同的方法获取另一个连接。警告:在一个多线程环境,不要使用下面的方法:
1.pool.connection().cursor().execute(...)
2.3.db=pool.connection()
4.5.cur=db.cursor()
6.7.cur.execute(...)
8.9.res=cur.fetchone()
10.11.cur.close()#ordelcur12.13.db.close()#ordeldb14.示例[方便你将来直接使用]使用PersistentDB模块1.importthreading,time,datetime2.3.importMySQLdb4.5.importDBUtils.PersistentDB6.7.persist=DBUtils.PersistentDB.PersistentDB(MySQLdb,100,host='localhost',user='root',passwd='321',db='test',charset='utf8')
8.9.conn=persist.connection()
10.11.cursor=conn.cursor()
12.13.cursor.execute("insertintomevalues(1,'22222')")
14.15.conn.commit()
16.17.conn.close()
18.通过以上的内容你就可以得到数据库连接了!作者:不详来源:网络