From 3128bcda3b50872f141606d09ad6ea5b242535f4 Mon Sep 17 00:00:00 2001 From: ClovertaTheTrilobita Date: Thu, 16 Jan 2025 01:16:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E5=BE=85=E5=8A=9E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=8A=9F=E8=83=BD=20=E8=8B=A5?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E8=A1=A8=E5=88=99=E5=AF=B9=E7=8E=B0=E6=9C=89?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=8D=E5=81=9A=E6=94=B9=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 1 + src/my_sqlite/data_init/data_init.py | 36 +++++++++++++++++++++++++++- src/my_sqlite/data_init/todo_init.py | 9 ++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 76bebd1..77746ea 100644 --- a/bot.py +++ b/bot.py @@ -12,6 +12,7 @@ def init_all(): # 初始化数据库 data_init.QrFortune_init() data_init.touch_init() + data_init.todo_init() if __name__ == "__main__": init_all() diff --git a/src/my_sqlite/data_init/data_init.py b/src/my_sqlite/data_init/data_init.py index 4356f84..c77f5de 100644 --- a/src/my_sqlite/data_init/data_init.py +++ b/src/my_sqlite/data_init/data_init.py @@ -62,9 +62,43 @@ def execute_init_file2(): except subprocess.CalledProcessError as 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';") # 查询初始化表是否存在 selectQrTouch = text( - "SELECT name FROM sqlite_master WHERE type='table' AND name='qr_touch';") \ No newline at end of file + "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';") \ No newline at end of file diff --git a/src/my_sqlite/data_init/todo_init.py b/src/my_sqlite/data_init/todo_init.py index c369325..5dace55 100644 --- a/src/my_sqlite/data_init/todo_init.py +++ b/src/my_sqlite/data_init/todo_init.py @@ -5,21 +5,20 @@ conn = sqlite3.connect('chat_bot.db') # 创建游标 c = conn.cursor() # -c.execute("""DROP TABLE IF EXISTS user_todo_list; """) -c.execute("""DROP TABLE IF EXISTS user_list; """) +# c.execute("""DROP TABLE IF EXISTS user_todo_list; """) +# c.execute("""DROP TABLE IF EXISTS user_list; """) # 创建表 c.execute(""" -CREATE TABLE user_list ( +CREATE TABLE IF NOT EXISTS user_list ( user_id VARCHAR(100) PRIMARY KEY ); """) -print("user_list created") c.execute(""" -CREATE TABLE user_todo_list ( +CREATE TABLE IF NOT EXISTS user_todo_list ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id VARCHAR(100), content TEXT,