-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
53 lines (48 loc) · 1.42 KB
/
database.py
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
import sqlite3
from datetime import datetime
# Function to initialize database
def initialize_database():
conn = sqlite3.connect('logs.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS predictions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model_name TEXT NOT NULL,
input_data TEXT NOT NULL,
result TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
# Function to insert a prediction log into database
def log_prediction(model_name, input_data, result):
conn = sqlite3.connect('logs.db')
c = conn.cursor()
c.execute('''
INSERT INTO predictions (model_name, input_data, result)
VALUES (?, ?, ?)
''', (model_name, str(input_data), result))
conn.commit()
conn.close()
# Function to retrieve all logs from database
def fetch_logs():
conn = sqlite3.connect('logs.db')
c = conn.cursor()
c.execute('''
SELECT model_name, input_data, result, timestamp FROM predictions ORDER BY timestamp DESC
''')
logs = c.fetchall()
conn.close()
return logs
# Function to clear all logs from database
def clear_logs():
conn = sqlite3.connect('logs.db')
c = conn.cursor()
c.execute('''
DELETE FROM predictions
''')
conn.commit()
conn.close()
# Initialize the database when the script runs
initialize_database()