From 8aede386c6ab2323110fafdbb81fc01a2426eb1f Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Fri, 25 Feb 2022 18:27:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 4 +- bin/db/db.py | 6 +++ bin/db/sqlite.py | 114 -------------------------------------------- bin/tests/MySQL_.py | 6 +++ 4 files changed, 14 insertions(+), 116 deletions(-) create mode 100644 bin/db/db.py delete mode 100644 bin/db/sqlite.py create mode 100644 bin/tests/MySQL_.py diff --git a/app.py b/app.py index 644b55e..7223793 100644 --- a/app.py +++ b/app.py @@ -21,7 +21,7 @@ from bin.utils.view import view_template from bin.utils.logger import logger from bin.utils.packing_logs import make_targz from bin.utils.settings import Settings -from bin.db.sqlite import Database +from bin.db.db import SQLite app = Flask(__name__, static_url_path='') app.config['JSON_SORT_KEYS'] = False # 设置JSON消息不根据字母顺序重新排序 @@ -36,7 +36,7 @@ def build_page(name: str, length: int, theme: str) -> list[bool or Response] or :param length: :return: """ - count = Database().fetch_data(name) + count = SQLite().fetch_data(name) if len(str(count)) > length: # 判断在数据库内的长度是否超过了设定的(或预设的)长度 return [False, ErrorProcess().too_lang_to_count(name)] if 7 <= length <= 10: # 判断设定的长度是否超过阈值 diff --git a/bin/db/db.py b/bin/db/db.py new file mode 100644 index 0000000..0b5349b --- /dev/null +++ b/bin/db/db.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 +# -- coding:utf-8 -- +# @Author: markushammered@gmail.com +# @Development Tool: PyCharm +# @Create Time: 2022/2/24 +# @File Name: db.py \ No newline at end of file diff --git a/bin/db/sqlite.py b/bin/db/sqlite.py deleted file mode 100644 index c4b4e46..0000000 --- a/bin/db/sqlite.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env python3 -# -- coding:utf-8 -- -# @Author: markushammered@gmail.com -# @Development Tool: PyCharm -# @Create Time: 2022/2/4 -# @File Name: sqlite.py - -import sqlite3 -from typing import Any -from bin.utils.settings import Settings - - -class Database: - def __init__(self): - self.sqlite_path = Settings().path - - def insert_data(self, name: str) -> None: - """ - 插入数据 - :param name: - :return: - """ - conn = sqlite3.connect(self.sqlite_path, check_same_thread=False) - cursor = conn.cursor() - try: - cursor.execute('insert into ReqCount values(?, ?)', (name, 1)) - conn.commit() - finally: - cursor.close() - conn.close() - - def fetch_data(self, name: str) -> int: - """ - 获取数据 - :param name: - :return: - """ - conn = sqlite3.connect(self.sqlite_path, check_same_thread=False) - cursor = conn.cursor() - try: - cursor.execute('select * from ReqCount') - conn.commit() - data = cursor.fetchall() - - temp_dict = {} - for k, v in data: # 遍历数据将元组数据转换为字典类型 - temp_dict.setdefault(k, []).append(v) - for i, c in zip(temp_dict.keys(), temp_dict.values()): - temp_dict[i] = c[0] - - if name in temp_dict.keys(): - count = temp_dict[name] # 获取原数字 为 整型 - self.update_data(name, count) - return count - else: - # 新建用户数据 - self.insert_data(name) - return 0 - finally: - cursor.close() - conn.close() - - def update_data(self, name: str, times: int) -> None: - """ - 更新数据 - :param name: - :param times: - :return: - """ - conn = sqlite3.connect(self.sqlite_path, check_same_thread=False) - cursor = conn.cursor() - try: - times += 1 - cursor.execute('update ReqCount set times=? where name=?', (times, name)) - conn.commit() - finally: - cursor.close() - conn.close() - - def fetch_table(self) -> list: - """ - 返回已有主题列表 - :return: - """ - conn_temp = sqlite3.connect(self.sqlite_path, check_same_thread=False) - cursor_temp = conn_temp.cursor() - try: - lst = [] - cursor_temp.execute("select * from sqlite_master where type='table'") - for i in cursor_temp.fetchall(): - lst.append(i[1]) - return lst - finally: - cursor_temp.close() - conn_temp.close() - - def fetch_style_data(self, style: str) -> list[dict[str, Any]]: - """ - 获取主题数据库内的数据 - :param style: - :return: - """ - conn = sqlite3.connect(self.sqlite_path, check_same_thread=False) - cursor = conn.cursor() - try: - cursor.execute('select * from %(style_name)s' % {'style_name': style}) - data = cursor.fetchall() - data_set = [] - for i in data: - data_set.append({'index': i[0], 'base64': i[1], 'width': i[2], 'height': i[3]}) - return data_set - finally: - cursor.close() - conn.close() diff --git a/bin/tests/MySQL_.py b/bin/tests/MySQL_.py new file mode 100644 index 0000000..88a09e5 --- /dev/null +++ b/bin/tests/MySQL_.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 +# -- coding:utf-8 -- +# @Author: markushammered@gmail.com +# @Development Tool: PyCharm +# @Create Time: 2022/2/25 +# @File Name: MySQL_.py \ No newline at end of file