- 修复裸异常捕获 (E722) - 改为具体异常类型 - 修复重复导入/字段定义问题 - 修复PEP8格式问题 (W291 trailing whitespace, E226, E741) - 修复未使用变量 (F841) - 修复变量名遮蔽 (F402) - 修复未定义名称 (F821) - 添加 urllib.parse 导入 - 修复 f-string 缺少占位符 (F541) - 修复模块级导入位置 (E402) - 修复行尾空白和空行问题 - 优化代码结构,提升可读性
46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Initialize database with schema"""
|
|
|
|
import sqlite3
|
|
import os
|
|
|
|
db_path = os.path.join(os.path.dirname(__file__), "insightflow.db")
|
|
schema_path = os.path.join(os.path.dirname(__file__), "schema.sql")
|
|
|
|
print(f"Database path: {db_path}")
|
|
print(f"Schema path: {schema_path}")
|
|
|
|
# Read schema
|
|
with open(schema_path, "r") as f:
|
|
schema = f.read()
|
|
|
|
# Execute schema
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# Split schema by semicolons and execute each statement
|
|
statements = schema.split(";")
|
|
success_count = 0
|
|
error_count = 0
|
|
|
|
for stmt in statements:
|
|
stmt = stmt.strip()
|
|
if stmt:
|
|
try:
|
|
cursor.execute(stmt)
|
|
success_count += 1
|
|
except sqlite3.Error as e:
|
|
# Ignore "already exists" errors
|
|
if "already exists" in str(e):
|
|
success_count += 1
|
|
else:
|
|
print(f"Error: {e}")
|
|
error_count += 1
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
print("\nSchema execution complete:")
|
|
print(f" Successful statements: {success_count}")
|
|
print(f" Errors: {error_count}")
|