mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-05-06 18:29:24 +08:00
移除对MySQL服务器的支持
This commit is contained in:
parent
7b66cbb7e4
commit
afdbdec803
62
db.py
62
db.py
@ -1,25 +1,18 @@
|
|||||||
import config , uuid
|
import config , uuid , pathlib
|
||||||
|
|
||||||
def getconn():
|
def getconn():
|
||||||
try:
|
try:
|
||||||
if config.readConf()["db"]["type"] == "sqlite3":
|
import sqlite3
|
||||||
import sqlite3
|
conn = sqlite3.connect(config.readConf()["db"]["path"])
|
||||||
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
|
return conn
|
||||||
except:
|
except:
|
||||||
print("DB ERROR")
|
print("DB ERROR")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def dbIsOK():
|
def dbIsOK():
|
||||||
#打开数据库连接
|
#打开数据库连接
|
||||||
if "init" not in config.readConf():
|
path = pathlib.Path(config.readConf()["db"]["path"])
|
||||||
config.updateConf({"init" : True})
|
if not path.is_file():
|
||||||
init()
|
init()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -53,10 +46,7 @@ def userSurplus(userkey): #查询userkey剩余配额
|
|||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 使用 execute() 方法执行 SQL 查询
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
if config.readConf()["db"]["type"] == "sqlite3":
|
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
|
||||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
|
|
||||||
else:
|
|
||||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = %s;",[userkey])
|
|
||||||
# 使用 fetchone() 方法获取单条数据.
|
# 使用 fetchone() 方法获取单条数据.
|
||||||
data = cursor.fetchone()
|
data = cursor.fetchone()
|
||||||
|
|
||||||
@ -74,10 +64,7 @@ def reduce_value(userkey, value): # 减去对应的值
|
|||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 执行 SQL 查询以获取当前值
|
# 执行 SQL 查询以获取当前值
|
||||||
if config.readConf()["db"]["type"] == "sqlite3":
|
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = ?;",[userkey])
|
||||||
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]
|
current_value = cursor.fetchone()[0]
|
||||||
|
|
||||||
# 如果没有找到用户,则返回错误信息
|
# 如果没有找到用户,则返回错误信息
|
||||||
@ -89,10 +76,7 @@ def reduce_value(userkey, value): # 减去对应的值
|
|||||||
new_value = current_value - value
|
new_value = current_value - value
|
||||||
|
|
||||||
# 更新数据库中的值
|
# 更新数据库中的值
|
||||||
if config.readConf()["db"]["type"] == "sqlite3":
|
cursor.execute(f"UPDATE usersurplus SET surplus= ? WHERE userkey= ?;",[new_value,userkey])
|
||||||
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])
|
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -127,10 +111,7 @@ def delKey(userkey):
|
|||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
# 使用 execute() 方法执行 SQL 查询
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
if config.readConf()["db"]["type"] == "sqlite3":
|
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = ?;", [userkey])
|
||||||
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = ?;", [userkey])
|
|
||||||
else:
|
|
||||||
cursor.execute(f"DELETE FROM usersurplus WHERE userkey = %s;", [userkey])
|
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -150,24 +131,15 @@ def createKey(quota,number=1,key="null"):
|
|||||||
|
|
||||||
# 使用 execute() 方法执行 SQL 查询
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
output = []
|
output = []
|
||||||
if config.readConf()["db"]["type"] == "sqlite3":
|
|
||||||
if key == "null":
|
if key == "null":
|
||||||
for i in range(int(number)):
|
for i in range(int(number)):
|
||||||
key = str(uuid.uuid1())
|
key = str(uuid.uuid1())
|
||||||
output.append(key)
|
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])
|
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
|
||||||
output.append(key)
|
|
||||||
else:
|
else:
|
||||||
if key == "null":
|
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (?, ?);", [key, quota])
|
||||||
for i in range(int(number)):
|
output.append(key)
|
||||||
key = str(uuid.uuid1())
|
|
||||||
output.append(key)
|
|
||||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
|
||||||
else:
|
|
||||||
cursor.execute(f"INSERT INTO usersurplus (userkey,surplus) VALUES (%s, %s);", [key, quota])
|
|
||||||
output.append(key)
|
|
||||||
|
|
||||||
|
|
||||||
# 提交事务
|
# 提交事务
|
||||||
|
2
main.py
2
main.py
@ -61,7 +61,7 @@ def login():
|
|||||||
if flask.request.method == 'GET':
|
if flask.request.method == 'GET':
|
||||||
return flask.render_template('login.html')
|
return flask.render_template('login.html')
|
||||||
userRequest = flask.request.form
|
userRequest = flask.request.form
|
||||||
if userRequest["password"] != config.readConf()["appconf"]["adminkey"]:
|
if userRequest["password"] != config.readConf()["adminkey"]:
|
||||||
return flask.render_template('login.html')
|
return flask.render_template('login.html')
|
||||||
flask.session["admin"] = True
|
flask.session["admin"] = True
|
||||||
return flask.redirect(flask.url_for('admin'))
|
return flask.redirect(flask.url_for('admin'))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user