pymysql封装

202 阅读1分钟
import pymysql
from timeit import default_timer

host = "localhost"
port = 3306
db_name = "smart_car"
user = "root"
password = ""


# ---- 用pymysql 操作数据库
def get_connection():
    conn = pymysql.connect(
        host=host, port=port, db=db_name, user=user, password=password
    )
    return conn


# ---- 使用 with 的方式来优化代码
class UsingMysql(object):
    def __init__(self, commit=True, log_time=True, log_label="总用时"):
        """

        :param commit: 是否在最后提交事务(设置为False的时候方便单元测试)
        :param log_time:  是否打印程序运行总时间
        :param log_label:  自定义log的文字
        """
        self._log_time = log_time
        self._commit = commit
        self._log_label = log_label

    def __enter__(self):
        # 如果需要记录时间
        if self._log_time is True:
            self._start = default_timer()

        # 在进入的时候自动获取连接和cursor
        conn = get_connection()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        conn.autocommit = False

        self._conn = conn
        self._cursor = cursor
        return self

    def __exit__(self, *exc_info):
        # 提交事务
        if self._commit:
            self._conn.commit()
        # 在退出的时候自动关闭连接和cursor
        self._cursor.close()
        self._conn.close()

        if self._log_time is True:
            diff = default_timer() - self._start
            print("-- %s: %.6f 秒" % (self._log_label, diff))

    @property
    def cursor(self):
        return self._cursor