From bdec6f64fc4db0e87788fbb474a41392189ab2bb Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Sun, 11 Sep 2022 10:31:35 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E5=B0=86=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=A7=BB=E5=8A=A8=E8=87=B3db.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/db.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/db/db.py diff --git a/src/db/db.py b/src/db/db.py new file mode 100644 index 0000000..51ca29c --- /dev/null +++ b/src/db/db.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +# -- coding:utf-8 -- +# @Author: markushammered@gmail.com +# @Development Tool: PyCharm +# @Create Time: 2022/9/11 +# @File Name: db.py + + +import os +import sqlite3 +from src.config import Config +from src.utils.t_download import download + + +class Database: + def __init__(self): + self.conn = None + self.cursor = None + + 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 + + +class SQLite(Database): + def __init__(self): + super().__init__() + self.conn = sqlite3.connect('./src/db/data.sqlite') + self.cursor = self.conn.cursor() + + +class MySQL(Database): + def __init__(self): + super().__init__() + user = os.getenv('m_user') + pwd = os.getenv('m_password') + host = os.getenv('m_host') + port = int(os.getenv('m_port')) + db = os.getenv('m_database') + self.conn = pymysql.connect(host=host, + user=user, + password=pwd, + port=port, + database=db + ) + self.cursor = self.conn.cursor() + + +if __name__ != '__main__': + if Config.database == 'sqlite': + print('Using SQLite Database') + 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.') + else: + print('Using MySQL Database') + if not os.getenv('m_user'): + import pymysql