-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
88 lines (75 loc) · 2.48 KB
/
Copy pathdatabase.py
File metadata and controls
88 lines (75 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sqlite3
import datetime
import os
# --- CONFIGURATION ---
# We use a NEW filename to avoid confusion with old files
DB_NAME = 'pcb_app_data.db'
# Use absolute path so python always finds it
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_DIR, DB_NAME)
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db():
"""Initializes the database if it doesn't exist."""
print(f"🔌 Checking Database at: {DB_PATH}")
conn = get_db_connection()
cursor = conn.cursor()
# Create the single table we need
cursor.execute('''
CREATE TABLE IF NOT EXISTS scans (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
status TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
defects_data TEXT,
confidence REAL,
defects_count INTEGER
)
''')
conn.commit()
conn.close()
print("✅ Database initialized successfully.")
def save_scan(filename, status, defects_list, max_conf):
"""Saves a new scan result."""
try:
conn = get_db_connection()
cursor = conn.cursor()
defects_str = str(defects_list)
defects_count = len(defects_list) if defects_list else 0
cursor.execute('''
INSERT INTO scans (filename, status, defects_data, confidence, defects_count)
VALUES (?, ?, ?, ?, ?)
''', (filename, status, defects_str, max_conf, defects_count))
conn.commit()
conn.close()
except Exception as e:
print(f"❌ Database Save Error: {e}")
def get_all_scans():
"""Fetches all history records."""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM scans ORDER BY id DESC")
scans = cursor.fetchall()
conn.close()
return scans
except Exception as e:
print(f"❌ Database Read Error: {e}")
return []
def clear_all_scans():
"""Wipes the database cleanly."""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("DELETE FROM scans")
conn.commit()
conn.close()
print(f"🗑️ CLEARED database at {DB_PATH}")
return True
except Exception as e:
print(f"❌ Database Delete Error: {e}")
return False
# Run initialization immediately when imported
init_db()