瀏覽代碼

增加sqllite模板打包

宋伯文 4 月之前
父節點
當前提交
ef4c6741fa
共有 3 個文件被更改,包括 34 次插入3 次删除
  1. 33 2
      common/sql_lite.py
  2. 1 1
      main.spec
  3. 二進制
      template.db

+ 33 - 2
common/sql_lite.py

@@ -1,12 +1,31 @@
+import atexit
+import os
+import pkgutil
+import shutil
 import sqlite3
+import sys
 from typing import Optional
 
+from common.log import logger
+
 connection: Optional[sqlite3.Connection] = None
 
 
 def init_global_db_connection(db_path='./local.db'):
     global connection
-    connection = sqlite3.connect(db_path)
+    output_directory = os.path.dirname(sys.executable)
+    db_file = os.path.join(output_directory, 'sqlite.db')
+    if not os.path.exists(db_file):
+        template_file = resource_path('template.db')
+        destination_file = os.path.join(output_directory, 'sqlite.db')
+
+        if template_file:
+            shutil.copyfile(template_file, destination_file)
+        else:
+            print(f"模板文件 template.db 不存在")
+    else:
+        print(f"{db_file} 已存在")
+    connection = sqlite3.connect('./sqlite.db')
     print("SQLite connection is established.")
 
 
@@ -22,5 +41,17 @@ def close_global_db_connection():
         print("SQLite connection is closed.")
 
 
-def init_new_db_connection(db_path='./local.db'):
+def init_new_db_connection(db_path='./sqlite.db'):
     return sqlite3.connect(db_path)
+
+
+def resource_path(relative_path):
+    # Function to get the absolute path to resources
+    try:
+        # PyInstaller creates a temp folder and stores path in _MEIPASS
+        base_path = sys._MEIPASS
+    except Exception:
+        base_path = os.path.abspath(".")
+
+    return os.path.join(base_path, relative_path)
+

+ 1 - 1
main.spec

@@ -5,7 +5,7 @@ a = Analysis(
     ['main.py'],
     pathex=[],
     binaries=[('config.json','config.json')],
-    datas=[('wcferry', 'wcferry')],
+    datas=[('wcferry', 'wcferry'),('template.db', '.')],
     hiddenimports=['_cffi_backend'],
     hookspath=[],
     hooksconfig={},

二進制
template.db