SPC/cleanup_db.py

35 lines
860 B
Python
Raw Permalink Normal View History

import sqlite3
import os
conn = sqlite3.connect('knowledge_base.db')
cursor = conn.cursor()
print("=== 清理无效的文档记录 ===\n")
cursor.execute('SELECT * FROM documents')
docs = cursor.fetchall()
for doc in docs:
doc_id, name, status, chunks, created_at = doc
print(f"检查文档: ID={doc_id}, Name={name}, Status={status}")
found = False
if os.path.exists('uploads'):
for file in os.listdir('uploads'):
if file.startswith(doc_id):
print(f" ✓ 找到文件: {file}")
found = True
break
if not found:
print(f" ✗ 未找到文件,删除记录")
cursor.execute('DELETE FROM documents WHERE id = ?', (doc_id,))
else:
print(f" ✓ 保留记录")
print()
conn.commit()
conn.close()
print("=== 清理完成 ===")