首页 > 初学python3 如何使用Dbutils

初学python3 如何使用Dbutils

from DBUtils.PooledDB import PooledDB
import MySQLdb
pool = PooledDB(creator=MySQLdb,
                mincached=1,
                maxcached=20,
                host='120.0.0.1',
                port=3306,
                user='root',
                passwd='XX',
                db='test')

这里creator是什么意思啊?MySQLdb是驱动吗,python3好像装不上,配置数据库参数这块要怎么弄啊?


In order to make use of the PooledDB module, you first need to set up the database connection pool by creating an instance of PooledDB, passing the following parameters:

creator: either an arbitrary function returning new DB-API 2 connection objects or a DB-API 2 compliant database module

mincached : the initial number of idle connections in the pool (the default of 0 means no connections are made at startup)

maxcached: the maximum number of idle connections in the pool (the default value of 0 or None means unlimited pool size)

maxshared: maximum number of shared connections allowed (the default value of 0 or None means all connections are dedicated)

When this maximum number is reached, connections are shared if they have been requested as shareable.

maxconnections: maximum number of connections generally allowed (the default value of 0 or None means any number of connections)

blocking: determines behavior when exceeding the maximum

If this is set to true, block and wait until the number of connections decreases, but by default an error will be reported.

maxusage: maximum number of reuses of a single connection (the default of 0 or None means unlimited reuse)

When this maximum usage number of the connection is reached, the connection is automatically reset (closed and reopened).

setsession: an optional list of SQL commands that may serve to prepare the session, e.g. ["set datestyle to german", ...]

reset: how connections should be reset when returned to the pool (False or None to rollback transcations started with begin(), the default value True always issues a rollback for safety's sake)

failures: an optional exception class or a tuple of exception classes for which the connection failover mechanism shall be applied, if the default (OperationalError, InternalError) is not adequate

ping: an optional flag controlling when connections are checked with the ping() method if such a method is available (0 = None = never, 1 = default = whenever fetched from the pool, 2 = when a cursor is created, 4 = when a query is executed, 7 = always, and all other bit combinations of these values)

The creator function or the connect function of the DB-API 2 compliant database module specified as the creator will receive any additional parameters such as the host, database, user, password etc. You may choose some or all of these parameters in your own creator function, allowing for sophisticated failover and load-balancing mechanisms.

For instance, if you are using pgdb as your DB-API 2 database module and want a pool of at least five connections to your local database mydb:

import pgdb # import used DB-API 2 module
from DBUtils.PooledDB import PooledDB
pool = PooledDB(pgdb, 5, database='mydb')

http://www.webwareforpython.org/DBUtils/Docs/UsersGuide.html#pooleddb

【热门文章】
【热门文章】