import pymysql
from timeit import default_timer
host = "localhost"
port = 3306
db_name = "smart_car"
user = "root"
password = ""
def get_connection():
conn = pymysql.connect(
host=host, port=port, db=db_name, user=user, password=password
)
return conn
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()
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()
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