zoukankan      html  css  js  c++  java
  • python数据库连接池

    安装DBUtils库:

    python3 -m pip install DBUtils去安装DBUtils库

     通过连接池的方式去创建数据库对象:

    这里参考我的上一篇博客:http://www.cnblogs.com/letmeiscool/p/8434381.html和DBUtils用户指南:http://blog.csdn.net/gashero/article/details/1577187去写。单独写了个创建连接池的方法,在创建数据库对象的时候被执行。之后每次去执行sql的时候,不需要去创建连接池,只需要每次执行sql前去执行连接方法_Getconnect,sql执行完毕,去关闭连接,连接被数据库连接池给回收。

    #-*-coding:utf-8-*-s
    #mysql和sqlserver的库
    import pymysql,pymssql
    from DBUtils.PooledDB import PooledDB
    class Database:
        def __init__(self,*db):
            if len(db)==5:
                #mysql数据库
                self.host=db[0]
                self.port=db[1]
                self.user=db[2]
                self.pwd=db[3]
                self.db=db[4]
            else:
                #sqlserver数据库
                self.host=db[0]
                self.port=None
                self.user=db[1]
                self.pwd=db[2]
                self.db=db[3]
            self._CreatePool()
        def _CreatePool(self):
            if not self.db:
                raise NameError+"没有设置数据库信息"
            if (self.port==None):
                self.Pool=PooledDB(creator=pymssql,mincached=2, maxcached=5,maxshared=3, maxconnections=6, blocking=True,host=self.host,user=self.user, 
                                   password=self.pwd,database=self.db,charset="utf8")
            else:
                self.Pool=PooledDB(creator=pymysql,mincached=2, maxcached=5,maxshared=3, maxconnections=6, blocking=True,host=self.host,port=self.port, 
                                   user=self.user,password=self.pwd,database =self.db,charset="utf8")
        def _Getconnect(self):
            self.conn=self.Pool.connection()
            cur=self.conn.cursor()
            if not cur:
                raise "数据库连接不上"
            else:
                return cur
        #查询sql
        def ExecQuery(self,sql):
            cur=self._Getconnect()
            cur.execute(sql)
            relist=cur.fetchall()
            cur.close()
            self.conn.close()
            return relist
        #非查询的sql
        def ExecNoQuery(self,sql):
            cur=self._Getconnect()
            cur.execute(sql)
            self.conn.commit()
            cur.close()
            self.conn.close()
    

      

    PooledDB的参数:
    • dbapi: 需要使用的DB-API 2模块
    • mincached : 启动时开启的空连接数量(缺省值 0 意味着开始时不创建连接)
    • maxcached: 连接池使用的最多连接数量(缺省值 0 代表不限制连接池大小)
    • maxshared: 最大允许的共享连接数量(缺省值 0 代表所有连接都是专用的)如果达到了最大数量,被请求为共享的连接将会被共享使用。
    • maxconnections: 最大允许连接数量(缺省值 0 代表不限制)
    • blocking: 设置在达到最大数量时的行为(缺省值 0 或 False 代表返回一个错误;其他代表阻塞直到连接数减少)
    • maxusage: 单个连接的最大允许复用次数(缺省值 0 或 False 代表不限制的复用)。当达到最大数值时,连接会自动重新连接(关闭和重新打开)
    • setsession: 一个可选的SQL命令列表用于准备每个会话,如 ["set datestyle to german", ...]
    • 其他,你可以设置用于传递到真正的DB-API 2的参数,例如主机名、数据库、用户名、密码等。
  • 相关阅读:
    微信小程序 单选按钮 最佳
    微信小程序 单选按钮的实现
    微信小程序 单选框实现
    Java Code To Create Pyramid and Pattern
    Java language
    npm Err! Unexpected end of JSON input while parsing near
    Node.js Express FrameWork Tutorial
    Higher-Order Function Examples
    Create First HTTP Web Server in Node.js: Complete Tutorial
    Node.js NPM Tutorial: Create, Publish, Extend & Manage
  • 原文地址:https://www.cnblogs.com/letmeiscool/p/8435923.html
Copyright © 2011-2022 走看看