-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
111 lines (79 loc) · 3.45 KB
/
database.py
File metadata and controls
111 lines (79 loc) · 3.45 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sqlite3
from datetime import datetime
class DrawingDatabase:
def __init__(self, db_path = "drawings.db"):
self.conn = sqlite3.connect(db_path)
self.create_tables()
def create_tables(self):
cursor = self.conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS Drawings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
gesture_id INTEGER
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS strokes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
drawing_id INTEGER,
color TEXT
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS Points (
id INTEGER PRIMARY KEY AUTOINCREMENT,
stroke_id INTEGER,
x INTEGER,
y INTEGER,
point_order INTEGER
)
""")
self.conn.commit()
def save_drawing(self, strokes, gesture_id):
cursor = self.conn.cursor()
timestamp = datetime.now().isoformat()
# 1. insert the new drawing
cursor.execute("INSERT INTO Drawings (timestamp, gesture_id) VALUES (?, ?)", (timestamp, gesture_id))
drawing_id = cursor.lastrowid
# 2. Insert strokes + points
for stroke_points, color in strokes:
cursor.execute("INSERT INTO Strokes (drawing_id, color) VALUES (?, ?)", (drawing_id, str(color)))
stroke_id = cursor.lastrowid
for order, (x,y) in enumerate(stroke_points):
cursor.execute(
"INSERT INTO Points (stroke_id, x, y, point_order) VALUES (?, ?, ?, ?)", (stroke_id, x, y, order)
)
self.conn.commit()
def load_last_drawing(self):
cursor = self.conn.cursor()
# 1. Get most recent drawing
cursor.execute("SELECT id FROM Drawings ORDER BY id DESC LIMIT 1")
row = cursor.fetchone()
if not row:
return []
drawing_id = row[0]
# 2. load strokes for that drawing
cursor.execute("SELECT id, color FROM Strokes WHERE drawing_id = ?", (drawing_id,))
strokes_data = cursor.fetchall()
loaded_strokes = []
for stroke_id, color_str in strokes_data:
# convert string like (255, 255, 255) back to tuple
color = eval(color_str)
# 3. load poionts
cursor.execute("SELECT x,y FROM Points WHERE stroke_id = ? ORDER BY point_order ASC", (stroke_id,))
points = cursor.fetchall()
loaded_strokes.append((points, color))
return loaded_strokes
def delete_last_stroke(self):
cursor = self.conn.cursor()
# Get last stroke inserted
cursor.execute("SELECT id FROM strokes ORDER BY id DESC LIMIT 1")
row = cursor.fetchone()
if not row:
return
stroke_id = row[0]
# Delete points forst (due to Foreign Key if used)
cursor.execute("DELETE FROM Points WHERE stroke_id = ?", (stroke_id, ))
cursor.execute("DELETE FROM Strokes WHERE id = ?", (stroke_id,))
self.conn.commit()