将数据库合并并添加了配置文件

This commit is contained in:
MarkusJoe
2022-02-23 22:04:21 +08:00
parent 7f723ddc59
commit 8f369fca03
11 files changed
+313 -254

No files matched your search

+1 -13
View File
@@ -6,16 +6,4 @@
# @File Name: __init__.py.py
import os
from bin.utils.logger import logger
if not os.path.exists('./bin/db/count.db'):
logger.error('未检测到用户计数数据库')
import sqlite3
conn = sqlite3.connect('./bin/db/count.db', check_same_thread=False)
cursor = conn.cursor()
cursor.execute('create table ReqCount (name text primary key, times int)')
conn.commit()
cursor.close()
conn.close()
logger.info('已在./db 目录下创建了count.db数据库')
__all__ = ['sqlite']
BIN
View File
Binary file not shown.
+96 -98
View File
@@ -7,110 +7,108 @@
import sqlite3
from typing import Any
from bin.utils.settings import Settings
def insert_data(name: str) -> None:
"""
插入数据
:param name:
:return:
"""
conn = sqlite3.connect('./bin/db/count.db', check_same_thread=False)
cursor = conn.cursor()
try:
cursor.execute('insert into ReqCount values(?, ?)', (name, 1))
conn.commit()
finally:
cursor.close()
conn.close()
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(name: str) -> int:
"""
获取数据
:param name:
:return:
"""
conn = sqlite3.connect('./bin/db/count.db', check_same_thread=False)
cursor = conn.cursor()
try:
cursor.execute('select * from ReqCount')
conn.commit()
data = cursor.fetchall()
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]
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] # 获取原数字 为 整型
update_data(name, count)
return count
else:
# 新建用户数据
insert_data(name)
return 0
finally:
cursor.close()
conn.close()
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 update_data(name: str, times: int) -> None:
"""
更新数据
:param name:
:param times:
:return:
"""
conn = sqlite3.connect('./bin/db/count.db', 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_table() -> list:
"""
返回已有主题列表
:return:
"""
conn_temp = sqlite3.connect('./bin/db/style.db', 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(style: str) -> list[dict[str, Any]]:
"""
获取主题数据库内的数据
:param style:
:return:
"""
conn = sqlite3.connect('./bin/db/style.db', 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()
__all__ = ['fetch_table', 'fetch_data', 'fetch_style_data', 'update_data', 'insert_data']
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()