-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_generation_script.py
More file actions
63 lines (53 loc) · 2 KB
/
log_generation_script.py
File metadata and controls
63 lines (53 loc) · 2 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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 15:42:36 2026
@author: jtey4
"""
import sqlite3
import random
import time
import os
import sys
def get_base_path():
#判斷是否為PyInstaller打包後的執行環境
if hasattr(sys, '_MEIPASS'):
#打包後,sys.executable指向.exe所在的實體路徑
return os.path.dirname(os.path.abspath(sys.executable))
else:
#開發環境,指向.py所在的實體路徑
return os.path.dirname(os.path.abspath(__file__))
#調用get_base_path函數來取得正確的基礎路徑
base_dir = get_base_path()
db_path = os.path.join(base_dir, 'production.db')
print(f"目前資料庫實際路徑:{db_path}")
def start_simulation():
#使用 with 確保連線自動關閉
with sqlite3.connect(db_path) as conn:
conn.execute("PRAGMA journal_mode=WAL;")
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS InspectionLogs (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
DefectType TEXT,
Confidence REAL
)
''')
print(f"開始模擬生產數據,寫入至 {db_path}...")
try:
while True:
data = {
"type": random.choice(["None", "Scratch", "Crack"]),
"score": round(random.uniform(0.7, 1.0), 2)
}
cursor.execute(
"INSERT INTO InspectionLogs (DefectType, Confidence) VALUES (:type, :score)",
data
)
conn.commit()
print(f"[{time.strftime('%H:%M:%S')}] 寫入: {data['type']} (信心度: {data['score']})")
time.sleep(1)
except KeyboardInterrupt:
print("模擬程式已手動停止。")
if __name__ == "__main__":
start_simulation()