update: 将数据库操作移动至db.py

This commit is contained in:
MarkusJoe
2022-09-11 10:31:21 +08:00
parent 731726371b
commit ba3a1f7cb5
1 file changed
+7 -48
+7 -48
View File
@@ -6,53 +6,12 @@
# @File Name: __init__.py.py # @File Name: __init__.py.py
import os from src.config import Config
import sqlite3
from src.utils.t_download import download if Config.database == 'sqlite':
from src.db.db import SQLite as Database
else:
from src.db.db import MySQL as Database
class SQLite: __all__ = [Database]
def __init__(self):
self.conn = sqlite3.connect('./src/db/data.sqlite')
# self.conn = sqlite3.connect('./data.sqlite')
self.cursor = self.conn.cursor()
def __del__(self):
self.conn.commit()
self.cursor.close()
self.conn.close()
def query(self, _id: str) -> tuple:
self.cursor.execute('select * from data where id="%s"' % _id)
result = self.cursor.fetchone()
if result is None:
self.insert(_id)
return tuple([_id, 0])
self.update(_id, result[1])
return result
def insert(self, _id: str) -> bool:
self.cursor.execute('insert into data (id, times) values ("%s", 1)' % _id)
return True
def update(self, _id: str, times: int) -> bool:
times += 1
self.cursor.execute('update data set times=%s where id="%s"' % (times, _id))
return True
def query_all(self) -> list:
self.cursor.execute('select * from data')
result = self.cursor.fetchall()
return result
def query_image(self, _id: str) -> list:
self.cursor.execute('select * from image where id="%s"' % _id)
result = self.cursor.fetchall()
return result
if __name__ != '__main__':
if not os.path.exists('./src/db/data.sqlite'):
print('database file not exists, start downloading...')
download('https://pac.rtst.tech/static_file_hosting/static/counter/data.sqlite')
print('Done.')