mirror of
				https://github.com/Kakune55/PyGetGPT.git
				synced 2025-11-04 13:44:40 +08:00 
			
		
		
		
	添加对SQLite3数据库的支持
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,2 +1,3 @@
 | 
			
		||||
/__pycache__
 | 
			
		||||
*.json
 | 
			
		||||
APPData.db
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								config.py
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								config.py
									
									
									
									
									
								
							@@ -3,3 +3,17 @@ import json
 | 
			
		||||
def readConf():
 | 
			
		||||
    with open('config.json') as f:
 | 
			
		||||
        return json.load(f)
 | 
			
		||||
 | 
			
		||||
def updateConf(data_dict):
 | 
			
		||||
    # 打开JSON文件并读取内容
 | 
			
		||||
    file_path = 'config.json'
 | 
			
		||||
    with open(file_path, 'r') as json_file:
 | 
			
		||||
        existing_data = json.load(json_file)
 | 
			
		||||
 | 
			
		||||
    # 将新的数据合并到现有的数据中
 | 
			
		||||
    existing_data.update(data_dict)
 | 
			
		||||
 | 
			
		||||
    # 再次打开文件(这次是以写模式),并将更新后的数据保存回文件
 | 
			
		||||
    with open(file_path, 'w') as json_file:
 | 
			
		||||
        json.dump(existing_data, json_file, indent=4, ensure_ascii=False)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										95
									
								
								db.py
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								db.py
									
									
									
									
									
								
							@@ -1,28 +1,61 @@
 | 
			
		||||
import pymysql , config , uuid
 | 
			
		||||
import config , uuid
 | 
			
		||||
 | 
			
		||||
def dbIsOK():
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
def getconn():
 | 
			
		||||
    try:
 | 
			
		||||
        db = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
        if config.readConf()["db"]["type"] == "sqlite3":
 | 
			
		||||
            import sqlite3
 | 
			
		||||
            conn = sqlite3.connect("APPData.db")
 | 
			
		||||
        elif config.readConf()["db"]["type"] == "mysql":
 | 
			
		||||
            import pymysql
 | 
			
		||||
            conn = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
                        port=config.readConf()["db"]["port"],
 | 
			
		||||
                        user=config.readConf()["db"]["user"],
 | 
			
		||||
                        password=config.readConf()["db"]["passwd"],
 | 
			
		||||
                        database=config.readConf()["db"]["database"])
 | 
			
		||||
        return conn
 | 
			
		||||
    except:
 | 
			
		||||
        print("DB ERROR")
 | 
			
		||||
        return 0
 | 
			
		||||
def dbIsOK():
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    if "init" not in config.readConf():
 | 
			
		||||
        config.updateConf({"init" : True})
 | 
			
		||||
        init()
 | 
			
		||||
        
 | 
			
		||||
    try:
 | 
			
		||||
        getconn()
 | 
			
		||||
        return True
 | 
			
		||||
    except:
 | 
			
		||||
        return False
 | 
			
		||||
    
 | 
			
		||||
def init():
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    db = getconn()
 | 
			
		||||
    # 使用 cursor() 方法创建一个游标对象 cursor
 | 
			
		||||
    cursor = db.cursor()
 | 
			
		||||
    #建表
 | 
			
		||||
    cursor.execute(
 | 
			
		||||
        '''
 | 
			
		||||
        CREATE TABLE usersurplus (
 | 
			
		||||
            userkey TEXT,
 | 
			
		||||
            surplus INT);
 | 
			
		||||
        ''')
 | 
			
		||||
    # 提交事务
 | 
			
		||||
    db.commit()
 | 
			
		||||
 | 
			
		||||
    # 关闭连接
 | 
			
		||||
    db.close()
 | 
			
		||||
 | 
			
		||||
def userSurplus(userkey): #查询userkey剩余配额
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    db = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
                     port=config.readConf()["db"]["port"],
 | 
			
		||||
                     user=config.readConf()["db"]["user"],
 | 
			
		||||
                     password=config.readConf()["db"]["passwd"],
 | 
			
		||||
                     database=config.readConf()["db"]["database"])
 | 
			
		||||
    db = getconn()
 | 
			
		||||
    # 使用 cursor() 方法创建一个游标对象 cursor
 | 
			
		||||
    cursor = db.cursor()
 | 
			
		||||
 
 | 
			
		||||
    # 使用 execute()  方法执行 SQL 查询 
 | 
			
		||||
    if config.readConf()["db"]["type"] == "sqlite3":
 | 
			
		||||
        cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
 | 
			
		||||
    else:
 | 
			
		||||
        cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
 | 
			
		||||
    # 使用 fetchone() 方法获取单条数据.
 | 
			
		||||
    data = cursor.fetchone()
 | 
			
		||||
@@ -36,15 +69,14 @@ def userSurplus(userkey): #查询userkey剩余配额
 | 
			
		||||
 | 
			
		||||
def reduce_value(userkey, value):  # 减去对应的值
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    db = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
                    port=config.readConf()["db"]["port"],
 | 
			
		||||
                    user=config.readConf()["db"]["user"],
 | 
			
		||||
                    password=config.readConf()["db"]["passwd"],
 | 
			
		||||
                    database=config.readConf()["db"]["database"])
 | 
			
		||||
    db = getconn()
 | 
			
		||||
    # 使用 cursor() 方法创建一个游标对象 cursor
 | 
			
		||||
    cursor = db.cursor()
 | 
			
		||||
 | 
			
		||||
    # 执行 SQL 查询以获取当前值
 | 
			
		||||
    if config.readConf()["db"]["type"] == "sqlite3":
 | 
			
		||||
        cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
 | 
			
		||||
    else:
 | 
			
		||||
        cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
 | 
			
		||||
    current_value = cursor.fetchone()[0]
 | 
			
		||||
 | 
			
		||||
@@ -57,6 +89,9 @@ def reduce_value(userkey, value):  # 减去对应的值
 | 
			
		||||
    new_value = current_value - value
 | 
			
		||||
 | 
			
		||||
    # 更新数据库中的值
 | 
			
		||||
    if config.readConf()["db"]["type"] == "sqlite3":
 | 
			
		||||
        cursor.execute(f"UPDATE usersurplus SET surplus= ? WHERE userkey= ?;",[new_value,userkey])
 | 
			
		||||
    else:
 | 
			
		||||
        cursor.execute(f"UPDATE usersurplus SET surplus= %s WHERE userkey= %s;",[new_value,userkey])
 | 
			
		||||
 | 
			
		||||
    # 提交事务
 | 
			
		||||
@@ -70,11 +105,7 @@ def reduce_value(userkey, value):  # 减去对应的值
 | 
			
		||||
 | 
			
		||||
def getAllKey():
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    db = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
                     port=config.readConf()["db"]["port"],
 | 
			
		||||
                     user=config.readConf()["db"]["user"],
 | 
			
		||||
                     password=config.readConf()["db"]["passwd"],
 | 
			
		||||
                     database=config.readConf()["db"]["database"])
 | 
			
		||||
    db = getconn()
 | 
			
		||||
    # 使用 cursor() 方法创建一个游标对象 cursor
 | 
			
		||||
    cursor = db.cursor()
 | 
			
		||||
 
 | 
			
		||||
@@ -91,15 +122,14 @@ def getAllKey():
 | 
			
		||||
 | 
			
		||||
def delKey(userkey):
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    db = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
                     port=config.readConf()["db"]["port"],
 | 
			
		||||
                     user=config.readConf()["db"]["user"],
 | 
			
		||||
                     password=config.readConf()["db"]["passwd"],
 | 
			
		||||
                     database=config.readConf()["db"]["database"])
 | 
			
		||||
    db = getconn()
 | 
			
		||||
    # 使用 cursor() 方法创建一个游标对象 cursor
 | 
			
		||||
    cursor = db.cursor()
 | 
			
		||||
 
 | 
			
		||||
    # 使用 execute()  方法执行 SQL 查询 
 | 
			
		||||
    if config.readConf()["db"]["type"] == "sqlite3":
 | 
			
		||||
        cursor.execute(f"DELETE FROM usersurplus WHERE userkey = ?;", [userkey])
 | 
			
		||||
    else:
 | 
			
		||||
        cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey])
 | 
			
		||||
 | 
			
		||||
    # 提交事务
 | 
			
		||||
@@ -114,16 +144,22 @@ def delKey(userkey):
 | 
			
		||||
 | 
			
		||||
def createKey(quota,number=1,key="null"):
 | 
			
		||||
    #打开数据库连接
 | 
			
		||||
    db = pymysql.connect(host=config.readConf()["db"]["host"],
 | 
			
		||||
                     port=config.readConf()["db"]["port"],
 | 
			
		||||
                     user=config.readConf()["db"]["user"],
 | 
			
		||||
                     password=config.readConf()["db"]["passwd"],
 | 
			
		||||
                     database=config.readConf()["db"]["database"])
 | 
			
		||||
    db = getconn()
 | 
			
		||||
    # 使用 cursor() 方法创建一个游标对象 cursor
 | 
			
		||||
    cursor = db.cursor()
 | 
			
		||||
 
 | 
			
		||||
    # 使用 execute()  方法执行 SQL 查询 
 | 
			
		||||
    output = []
 | 
			
		||||
    if config.readConf()["db"]["type"] == "sqlite3":
 | 
			
		||||
        if key == "null":
 | 
			
		||||
            for i in range(int(number)):
 | 
			
		||||
                key = str(uuid.uuid1())
 | 
			
		||||
                output.append(key)
 | 
			
		||||
                cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
 | 
			
		||||
        else:
 | 
			
		||||
            cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
 | 
			
		||||
            output.append(key)
 | 
			
		||||
    else:
 | 
			
		||||
        if key == "null":
 | 
			
		||||
            for i in range(int(number)):
 | 
			
		||||
                key = str(uuid.uuid1())
 | 
			
		||||
@@ -133,6 +169,7 @@ def createKey(quota,number=1,key="null"):
 | 
			
		||||
            cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
 | 
			
		||||
            output.append(key)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # 提交事务
 | 
			
		||||
    db.commit()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user