From f997bb611e7f5cd3e42e140836428787b62518bb Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Tue, 1 Mar 2022 22:33:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E4=BD=BF=E7=94=A8MySQL=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84bug,?= =?UTF-8?q?=20=E7=8E=B0=E5=B7=B2=E6=AD=A3=E5=BC=8F=E6=94=AF=E6=8C=81MySQL?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/db/db.py | 27 +++++++++++---------------- bin/utils/__init__.py | 10 +++++++++- bin/utils/settings.py | 10 +++++----- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/bin/db/db.py b/bin/db/db.py index f6ebbad..2f45040 100644 --- a/bin/db/db.py +++ b/bin/db/db.py @@ -8,20 +8,21 @@ import sqlite3 import pymysql +from bin.utils.settings import Settings + +conf = Settings() class SQLite: """操作SQLite数据库""" - def __init__(self, - path: str = './bin/db/data.db' - ) -> None: + def __init__(self, **kwargs) -> None: """ 初始化SQLite对象 完成后会自动提交, 自动关闭 :param path: 数据库路径 """ - self.__path = path + self.__path = './bin/db/data.db' self.__conn = sqlite3.connect(self.__path) self.__cursor = self.__conn.cursor() @@ -147,12 +148,7 @@ class SQLite: class MySQL: """操作MySQL数据库""" - def __init__(self, - host: str, - user: str, - pwd: str, - database: str - ) -> None: + def __init__(self, **kwargs) -> None: """ 初始化MySQL对象并创建一个连接 创建的连接会在执行完毕后自动提交以及自动关闭 @@ -161,10 +157,10 @@ class MySQL: :param pwd: 数据库密码 :param database: 数据库名 """ - self.__host = host - self.__user = user - self.__password = pwd - self.__database = database + self.__host = conf.m_host + self.__user = conf.m_user + self.__password = conf.m_pwd + self.__database = conf.m_db self.__conn = pymysql.connect(user=self.__user, password=self.__password, host=self.__host, @@ -268,7 +264,7 @@ class MySQL: :return: """ self.__cursor.execute( - 'update reqcount set times="%(times)s" where name="%(name)s"";' % {'times': times + 1, 'name': name}) + 'update reqcount set times=%(times)s where name="%(name)s";' % {'times': times + 1, 'name': name}) return True def fetching_table(self, table: str) -> list: @@ -292,4 +288,3 @@ class MySQL: __all__ = ['SQLite', 'MySQL'] - diff --git a/bin/utils/__init__.py b/bin/utils/__init__.py index 70c1dc7..02eda15 100644 --- a/bin/utils/__init__.py +++ b/bin/utils/__init__.py @@ -6,4 +6,12 @@ # @File Name: __init__.py -__all__ = ['error', 'logger', 'packing_logs', 'view'] +import os +from bin.utils.logger import logger +from bin.utils.settings import Settings + +if __name__ != '__main__': + if Settings().type == 'MySQL' and not os.path.exists('./bin/db/origin.sql'): + logger.info('当前使用的数据库为MySQL请前往 https://themedatabase.vercel.app/source/sql 下载sql文件') + logger.info('并使用 source 命令来导入MySQL数据库') + logger.info('忽略此消息请在./bin/db内创建一个名为origin.sql的文件') diff --git a/bin/utils/settings.py b/bin/utils/settings.py index 30d5da7..909d559 100644 --- a/bin/utils/settings.py +++ b/bin/utils/settings.py @@ -59,7 +59,7 @@ class Settings: return self.__database['type'] @property - def mysql_host(self) -> str: + def m_host(self) -> str: """ mysql数据库的地址 :return: @@ -67,7 +67,7 @@ class Settings: return self.__database['MySQL']['host'] @property - def mysql_port(self) -> int: + def m_port(self) -> int: """ mysql数据库的端口 默认3306 @@ -76,7 +76,7 @@ class Settings: return self.__database['MySQL']['port'] @property - def mysql_user(self) -> str: + def m_user(self) -> str: """ mysql数据库的用户名 :return: @@ -84,7 +84,7 @@ class Settings: return self.__database['MySQL']['user'] @property - def mysql_pwd(self) -> str: + def m_pwd(self) -> str: """ mysql数据库的密码 :return: @@ -92,7 +92,7 @@ class Settings: return self.__database['MySQL']['password'] @property - def mysql_db(self) -> str: + def m_db(self) -> str: """ 保存数据库数据库名 :return: