mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
添加第一次启动初始化待办数据库功能
若存在表则对现有数据不做改变
This commit is contained in:
parent
e86e7af83e
commit
3128bcda3b
3 changed files with 40 additions and 6 deletions
1
bot.py
1
bot.py
|
|
@ -12,6 +12,7 @@ def init_all():
|
||||||
# 初始化数据库
|
# 初始化数据库
|
||||||
data_init.QrFortune_init()
|
data_init.QrFortune_init()
|
||||||
data_init.touch_init()
|
data_init.touch_init()
|
||||||
|
data_init.todo_init()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
init_all()
|
init_all()
|
||||||
|
|
|
||||||
|
|
@ -62,9 +62,43 @@ def execute_init_file2():
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"执行初始化文件时出错: {e}")
|
print(f"执行初始化文件时出错: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def todo_init():
|
||||||
|
session = SqliteSqlalchemy().session
|
||||||
|
# 检查某个表是否存在
|
||||||
|
table_exists1 = session.execute(selectTodoTable).fetchone()
|
||||||
|
table_exists2 = session.execute(selectUserList).fetchone()
|
||||||
|
if table_exists1 and table_exists2:
|
||||||
|
return print("用户待办表状态正常。")
|
||||||
|
else:
|
||||||
|
print("待办功能未初始化,开始执行初始化文件。")
|
||||||
|
execute_init_file3()
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
执行初始化文件todo_init.py
|
||||||
|
"""
|
||||||
|
def execute_init_file3():
|
||||||
|
# 拼接文件的完整路径
|
||||||
|
file_path = os.getcwd() + "\\src\\my_sqlite\\data_init\\todo_init.py"
|
||||||
|
init_file_path = os.path.join(os.path.dirname(__file__), file_path)
|
||||||
|
try:
|
||||||
|
# 执行初始化文件
|
||||||
|
subprocess.run(["python", init_file_path], check=True)
|
||||||
|
print("初始化文件已成功执行。")
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(f"执行初始化文件时出错: {e}")
|
||||||
|
|
||||||
# 查询初始化表是否存在
|
# 查询初始化表是否存在
|
||||||
selectQrFortune = text( "SELECT name FROM sqlite_master WHERE type='table' AND name='qr_fortune';")
|
selectQrFortune = text( "SELECT name FROM sqlite_master WHERE type='table' AND name='qr_fortune';")
|
||||||
|
|
||||||
# 查询初始化表是否存在
|
# 查询初始化表是否存在
|
||||||
selectQrTouch = text(
|
selectQrTouch = text(
|
||||||
"SELECT name FROM sqlite_master WHERE type='table' AND name='qr_touch';")
|
"SELECT name FROM sqlite_master WHERE type='table' AND name='qr_touch';")
|
||||||
|
|
||||||
|
# 查询待办表是否存在
|
||||||
|
selectTodoTable = text(
|
||||||
|
"SELECT name FROM sqlite_master WHERE type='table' AND name='user_todo_list';")
|
||||||
|
selectUserList = text(
|
||||||
|
"SELECT name FROM sqlite_master WHERE type='table' AND name='user_list';")
|
||||||
|
|
@ -5,21 +5,20 @@ conn = sqlite3.connect('chat_bot.db')
|
||||||
# 创建游标
|
# 创建游标
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
#
|
#
|
||||||
c.execute("""DROP TABLE IF EXISTS user_todo_list; """)
|
# c.execute("""DROP TABLE IF EXISTS user_todo_list; """)
|
||||||
c.execute("""DROP TABLE IF EXISTS user_list; """)
|
# c.execute("""DROP TABLE IF EXISTS user_list; """)
|
||||||
# 创建表
|
# 创建表
|
||||||
c.execute("""
|
c.execute("""
|
||||||
|
|
||||||
CREATE TABLE user_list (
|
CREATE TABLE IF NOT EXISTS user_list (
|
||||||
user_id VARCHAR(100) PRIMARY KEY
|
user_id VARCHAR(100) PRIMARY KEY
|
||||||
);
|
);
|
||||||
|
|
||||||
""")
|
""")
|
||||||
print("user_list created")
|
|
||||||
|
|
||||||
c.execute("""
|
c.execute("""
|
||||||
|
|
||||||
CREATE TABLE user_todo_list (
|
CREATE TABLE IF NOT EXISTS user_todo_list (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
user_id VARCHAR(100),
|
user_id VARCHAR(100),
|
||||||
content TEXT,
|
content TEXT,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue