Dish_Recognition/dao/create_sql.py

104 lines
4.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from PyQt5 import QtSql
from PyQt5.QtSql import QSqlQuery
import sqlite3
# 初始化数据库连接(与您原有方式一致)
database = QtSql.QSqlDatabase.addDatabase("QSQLITE")
globe_database = r"monitor.db" # 您可以重命名为 dish_cache.db
database.setDatabaseName(globe_database)
database.open()
def create_tables():
"""
创建菜品识别本地缓存所需的所有表
"""
query = QSqlQuery()
# 1. 模板缓存表(核心,用于快速匹配)
sql_template_cache = '''
CREATE TABLE IF NOT EXISTS template_cache (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
category CHAR(100) NOT NULL, -- 菜品名称,如“青椒香干肉丝”
feature_blob BLOB NOT NULL, -- 存储 numpy 特征向量的二进制数据 (float32)
source_base CHAR(50), -- 该模板来源于哪个基地(可选)
server_version DATETIME, -- 总服务器上的最后更新时间,用于增量同步
is_active INTEGER DEFAULT 1, -- 是否启用(软删除)
create_time DATETIME DEFAULT CURRENT_TIMESTAMP
);
'''
if query.exec_(sql_template_cache):
print("✅ 表 template_cache 创建成功")
else:
print(f"❌ 创建 template_cache 失败: {query.lastError().text()}")
# 2. 待学习表(存储未能识别的图片)
sql_pending_learning = '''
CREATE TABLE IF NOT EXISTS pending_learning (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
image_path CHAR(255) NOT NULL, -- 原始图片路径
feature_blob BLOB NOT NULL, -- 该图片的特征向量
capture_time DATETIME DEFAULT CURRENT_TIMESTAMP,
status CHAR(20) DEFAULT 'pending', -- 'pending' / 'learned' / 'ignored'
category CHAR(100), -- 如果人工标注了类别,则填这里
base_id CHAR(50) -- 所属基地
);
'''
if query.exec_(sql_pending_learning):
print("✅ 表 pending_learning 创建成功")
else:
print(f"❌ 创建 pending_learning 失败: {query.lastError().text()}")
# 3. 同步状态表(键值对,记录上次同步时间等)
sql_sync_status = '''
CREATE TABLE IF NOT EXISTS sync_status (
key CHAR(50) PRIMARY KEY NOT NULL,
value CHAR(200) NOT NULL
);
'''
if query.exec_(sql_sync_status):
print("✅ 表 sync_status 创建成功")
# 初始化一条记录:上次同步时间(默认 1970-01-01)
init_sql = "INSERT OR IGNORE INTO sync_status (key, value) VALUES ('last_sync_time', '1970-01-01 00:00:00')"
if query.exec_(init_sql):
print(" - 初始化同步时间戳成功")
else:
print(f"❌ 创建 sync_status 失败: {query.lastError().text()}")
# 4. 上传队列表(本地新增模板,待上报总服务器)
sql_upload_queue = '''
CREATE TABLE IF NOT EXISTS upload_queue (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
category CHAR(100) NOT NULL,
feature_blob BLOB NOT NULL,
image_path CHAR(255), -- 原始图片路径(可选)
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
status CHAR(20) DEFAULT 'pending', -- 'pending' / 'uploaded' / 'failed'
retry_count INTEGER DEFAULT 0
);
'''
if query.exec_(sql_upload_queue):
print("✅ 表 upload_queue 创建成功")
else:
print(f"❌ 创建 upload_queue 失败: {query.lastError().text()}")
# 创建索引以提升查询性能
index_sqls = [
"CREATE INDEX IF NOT EXISTS idx_cache_category ON template_cache(category);",
"CREATE INDEX IF NOT EXISTS idx_pending_status ON pending_learning(status);",
"CREATE INDEX IF NOT EXISTS idx_upload_status ON upload_queue(status);",
]
for sql in index_sqls:
if not query.exec_(sql):
print(f"⚠️ 创建索引失败: {query.lastError().text()}")
print("🎯 所有表创建完成!")
# 调用建表函数
if __name__ == "__main__":
create_tables()
# 关闭连接(生产环境中根据生命周期管理)
database.close()