python DbUtils 封裝

python dbutils 簡介及準備工做

dbutils封裝文件傳送門python

DBUtils是一套Python數據庫鏈接池包,並容許對非線程安全的數據庫接口進行線程安全包裝。DBUtils來自Webware for Python。mysql

DBUtils提供兩種外部接口:sql

  • PersistentDB :提供線程專用的數據庫鏈接,並自動管理鏈接。
  • PooledDB :提供線程間可共享的數據庫鏈接,並自動管理鏈接。
    須要庫
    一、DBUtils pip install DBUtils
    二、pymysql pip install pymysql/MySQLdb

建立DButils組件數據庫

db_config.py 配置文件

# -*- coding: UTF-8 -*-
import pymysql

# 數據庫信息
DB_TEST_HOST = "127.0.0.1"
DB_TEST_PORT = 3306
DB_TEST_DBNAME = "ball"
DB_TEST_USER = "root"
DB_TEST_PASSWORD = "123456"

# 數據庫鏈接編碼
DB_CHARSET = "utf8"

# mincached : 啓動時開啓的閒置鏈接數量(缺省值 0 開始時不建立鏈接)
DB_MIN_CACHED = 10

# maxcached : 鏈接池中容許的閒置的最多鏈接數量(缺省值 0 表明不閒置鏈接池大小)
DB_MAX_CACHED = 10

# maxshared : 共享鏈接數容許的最大數量(缺省值 0 表明全部鏈接都是專用的)若是達到了最大數量,被請求爲共享的鏈接將會被共享使用
DB_MAX_SHARED = 20

# maxconnecyions : 建立鏈接池的最大數量(缺省值 0 表明不限制)
DB_MAX_CONNECYIONS = 100

# blocking : 設置在鏈接池達到最大數量時的行爲(缺省值 0 或 False 表明返回一個錯誤<toMany......> 其餘表明阻塞直到鏈接數減小,鏈接被分配)
DB_BLOCKING = True

# maxusage : 單個鏈接的最大容許複用次數(缺省值 0 或 False 表明不限制的複用).當達到最大數時,鏈接會自動從新鏈接(關閉和從新打開)
DB_MAX_USAGE = 0

# setsession : 一個可選的SQL命令列表用於準備每一個會話,如["set datestyle to german", ...]
DB_SET_SESSION = None

# creator : 使用鏈接數據庫的模塊
DB_CREATOR = pymysql

db_dbutils_init.py 建立數據池初始化

from DBUtils.PooledDB import PooledDB
import db_config as config

"""
@功能:建立數據庫鏈接池
"""


class MyConnectionPool(object):
    __pool = None

    # def __init__(self):
    #     self.conn = self.__getConn()
    #     self.cursor = self.conn.cursor()

    # 建立數據庫鏈接conn和遊標cursor
    def __enter__(self):
        self.conn = self.__getconn()
        self.cursor = self.conn.cursor()

    # 建立數據庫鏈接池
    def __getconn(self):
        if self.__pool is None:
            self.__pool = PooledDB(
                creator=config.DB_CREATOR,
                mincached=config.DB_MIN_CACHED,
                maxcached=config.DB_MAX_CACHED,
                maxshared=config.DB_MAX_SHARED,
                maxconnections=config.DB_MAX_CONNECYIONS,
                blocking=config.DB_BLOCKING,
                maxusage=config.DB_MAX_USAGE,
                setsession=config.DB_SET_SESSION,
                host=config.DB_TEST_HOST,
                port=config.DB_TEST_PORT,
                user=config.DB_TEST_USER,
                passwd=config.DB_TEST_PASSWORD,
                db=config.DB_TEST_DBNAME,
                use_unicode=False,
                charset=config.DB_CHARSET
            )
        return self.__pool.connection()

    # 釋放鏈接池資源
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.cursor.close()
        self.conn.close()

    # 關閉鏈接歸還給連接池
    # def close(self):
    #     self.cursor.close()
    #     self.conn.close()

    # 從鏈接池中取出一個鏈接
    def getconn(self):
        conn = self.__getconn()
        cursor = conn.cursor()
        return cursor, conn


# 獲取鏈接池,實例化
def get_my_connection():
    return MyConnectionPool()

製做mysqlhelper.py

from db_dbutils_init import get_my_connection

"""執行語句查詢有結果返回結果沒有返回0;增/刪/改返回變動數據條數,沒有返回0"""


class MySqLHelper(object):
    def __init__(self):
        self.db = get_my_connection()  # 從數據池中獲取鏈接

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, 'inst'):  # 單例
            cls.inst = super(MySqLHelper, cls).__new__(cls, *args, **kwargs)
        return cls.inst

    # 封裝執行命令
    def execute(self, sql, param=None, autoclose=False):
        """
        【主要判斷是否有參數和是否執行完就釋放鏈接】
        :param sql: 字符串類型,sql語句
        :param param: sql語句中要替換的參數"select %s from tab where id=%s" 其中的%s就是參數
        :param autoclose: 是否關閉鏈接
        :return: 返回鏈接conn和遊標cursor
        """
        cursor, conn = self.db.getconn()  # 從鏈接池獲取鏈接
        count = 0
        try:
            # count : 爲改變的數據條數
            if param:
                count = cursor.execute(sql, param)
            else:
                count = cursor.execute(sql)
            conn.commit()
            if autoclose:
                self.close(cursor, conn)
        except Exception as e:
            pass
        return cursor, conn, count

    # 執行多條命令
    # def executemany(self, lis):
    #     """
    #     :param lis: 是一個列表,裏面放的是每一個sql的字典'[{"sql":"xxx","param":"xx"}....]'
    #     :return:
    #     """
    #     cursor, conn = self.db.getconn()
    #     try:
    #         for order in lis:
    #             sql = order['sql']
    #             param = order['param']
    #             if param:
    #                 cursor.execute(sql, param)
    #             else:
    #                 cursor.execute(sql)
    #         conn.commit()
    #         self.close(cursor, conn)
    #         return True
    #     except Exception as e:
    #         print(e)
    #         conn.rollback()
    #         self.close(cursor, conn)
    #         return False

    # 釋放鏈接
    def close(self, cursor, conn):
        """釋放鏈接歸還給鏈接池"""
        cursor.close()
        conn.close()

    # 查詢全部
    def selectall(self, sql, param=None):
        try:
            cursor, conn, count = self.execute(sql, param)
            res = cursor.fetchall()
            return res
        except Exception as e:
            print(e)
            self.close(cursor, conn)
            return count

    # 查詢單條
    def selectone(self, sql, param=None):
        try:
            cursor, conn, count = self.execute(sql, param)
            res = cursor.fetchone()
            self.close(cursor, conn)
            return res
        except Exception as e:
            print("error_msg:", e.args)
            self.close(cursor, conn)
            return count

    # 增長
    def insertone(self, sql, param):
        try:
            cursor, conn, count = self.execute(sql, param)
            # _id = cursor.lastrowid()  # 獲取當前插入數據的主鍵id,該id應該爲自動生成爲好
            conn.commit()
            self.close(cursor, conn)
            return count
            # 防止表中沒有id返回0
            # if _id == 0:
            #     return True
            # return _id
        except Exception as e:
            print(e)
            conn.rollback()
            self.close(cursor, conn)
            return count

    # 增長多行
    def insertmany(self, sql, param):
        """
        :param sql:
        :param param: 必須是元組或列表[(),()]或((),())
        :return:
        """
        cursor, conn, count = self.db.getconn()
        try:
            cursor.executemany(sql, param)
            conn.commit()
            return count
        except Exception as e:
            print(e)
            conn.rollback()
            self.close(cursor, conn)
            return count

    # 刪除
    def delete(self, sql, param=None):
        try:
            cursor, conn, count = self.execute(sql, param)
            self.close(cursor, conn)
            return count
        except Exception as e:
            print(e)
            conn.rollback()
            self.close(cursor, conn)
            return count

    # 更新
    def update(self, sql, param=None):
        try:
            cursor, conn, count = self.execute(sql, param)
            conn.commit()
            self.close(cursor, conn)
            return count
        except Exception as e:
            print(e)
            conn.rollback()
            self.close(cursor, conn)
            return count


if __name__ == '__main__':
    db = MySqLHelper()
    # # 查詢單條
    # sql1 = 'select * from userinfo where name=%s'
    # args = 'python'
    # ret = db.selectone(sql=sql1, param=args)
    # print(ret)  # (None, b'python', b'123456', b'0')
    # 增長單條
    # sql2 = 'insert into userinfo (name,password) VALUES (%s,%s)'
    # ret = db.insertone(sql2, ('old2','22222'))
    # print(ret)
    # 增長多條
    # sql3 = 'insert into userinfo (name,password) VALUES (%s,%s)'
    # li = li = [
    #     ('分省', '123'),
    #     ('到達','456')
    # ]
    # ret = db.insertmany(sql3,li)
    # print(ret)
    # 刪除
    # sql4 = 'delete from  userinfo WHERE name=%s'
    # args = 'xxxx'
    # ret = db.delete(sql4, args)
    # print(ret)
    # 更新
    # sql5 = r'update userinfo set password=%s WHERE name LIKE %s'
    # args = ('993333993', '%old%')
    # ret = db.update(sql5, args)
    # print(ret)
相關文章
相關標籤/搜索