From c69a9122297ac3937789f6fe03161bdc18a6aa9d Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Thu, 16 Apr 2026 10:51:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E6=94=AF=E6=8C=81=20-=20=E8=87=AA=E5=8A=A8=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=BC=BA=E5=A4=B1=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/database.py b/database.py index 08b61d1..e36e4f6 100644 --- a/database.py +++ b/database.py @@ -15,9 +15,32 @@ class Database: self._init_db() def _init_db(self): - """初始化数据库""" + """初始化数据库(支持迁移)""" conn = sqlite3.connect(self.db_path) - conn.executescript(DB_SCHEMA) + + # 检查表是否存在 + cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='images'") + table_exists = cursor.fetchone() is not None + + if not table_exists: + # 新建数据库 + conn.executescript(DB_SCHEMA) + else: + # 迁移:检查是否有 date_folder 字段 + cursor = conn.execute("PRAGMA table_info(images)") + columns = [row[1] for row in cursor.fetchall()] + + if 'date_folder' not in columns: + conn.execute("ALTER TABLE images ADD COLUMN date_folder TEXT") + + if 'camera_id' not in columns: + conn.execute("ALTER TABLE images ADD COLUMN camera_id INTEGER DEFAULT 0") + + # 检查 config 表 + cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='config'") + if cursor.fetchone() is None: + conn.execute("CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT)") + conn.commit() conn.close()