Dbutils:
ImportPymysql fromDbutils.pooleddbImportPooleddb, Shareddbconnection#Defining pool Connection PoolsPOOL =Pooleddb (creator=pymysql,#modules that use linked databasesMaxconnections=6,#the maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connectionsmincached=2,#at least 0 of the free links created in the link pool are not created when initializingMaxcached=5,#most idle links in the link pool, 0 and none are not limitedMaxshared=3, #The maximum number of links shared in a linked pool, 0 and none means sharing all. PS: Useless, because Pymysql and mysqldb modules such as threadsafety are 1, all values regardless of set to how much, _maxcached forever is 0, so forever is all links are shared. Blocking=true,#If there are no available connections in the connection pool, wait is blocked. True, wait, False, not wait and then errorMaxusage=none,#the maximum number of times a link is reused, none means unlimitedSetsession=[],#a list of commands to execute before starting the session. such as: ["Set Datestyle to ...", "Set time zone ..."]ping=0,#Ping the MySQL server to check if the service is available. # for example: 0 = None = never, 1 = default = Whenever it is requested, 2 = When a cursor is created, 4 = When a query is executed, 7 = Alwayshost='127.0.0.1', Port=3306, the user='Root', Password='123', Database='Db_conn', CharSet='UTF8')deffunc (): Conn=pool.connection () cursor=conn.cursor (pymysql.cursors.DictCursor) Cursor.execute ('SELECT * from user') Result=Cursor.fetchall ()#print (Result) #print (list result )cursor.close () conn.close ( )
Use the connection pool to change the additions and deletions
fromDb_demoImportDb_connImportPymysql#Create a function to open a connection pooldefOpen_conn (): Conn=Db_conn. Pool.connection () cur=conn.cursor (pymysql.cursors.DictCursor)returnConn, cur#function to close the connection pooldefclose_conn (cur, conn): Cur.close () conn.close ()#View all information for a tabledefselect_all (sql, args): conn, cur=Open_conn () cur.execute (SQL, args) result=Cur.fetchall () close_conn (cur, conn)returnresult#Insert Table Functiondefinsert_help (sql, args): conn, cur=Open_conn () cur.execute (SQL, args) result=Conn.commit () close_conn (cur, conn)returnResult
View Code
Dbutil Configuring the Database connection pool