-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
103 lines (80 loc) · 3.15 KB
/
Copy pathsql.py
File metadata and controls
103 lines (80 loc) · 3.15 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
# sql.py
# Responsible for managing the internal SQL server that syncs smart devices across the home
import time
import sqlite3
def thread():
setup()
def setup():
print("SQL Lite 3 SETUP RUNNING...")
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
# Create tables
cursor.execute('''CREATE TABLE IF NOT EXISTS clients (id TEXT PRIMARY KEY, name TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS widgets (id TEXT PRIMARY KEY, name TEXT, class TEXT, css_name TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS client_layouts (
client_id TEXT,
widget_id TEXT,
row INTEGER,
col INTEGER,
FOREIGN KEY(client_id) REFERENCES clients(id),
FOREIGN KEY(widget_id) REFERENCES widgets(id))''')
cursor.execute('''CREATE TABLE IF NOT EXISTS widgetPrefs (
client_id TEXT,
widget_id TEXT,
prefs TEXT,
FOREIGN KEY(client_id) REFERENCES clients(id),
FOREIGN KEY(widget_id) REFERENCES widgets(id))''')
conn.commit()
conn.close()
print("...SQL Lite 3 SETUP COMPLETE")
def get_widget_preferences(client_id, widget_id):
try:
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
query = """SELECT widgetPrefs WHERE client_id = ? AND widget_id = ?"""
pref = cursor.execute(query, (client_id, widget_id))
if(pref):
return pref
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
conn.close()
return None
def update_widget_preference(client_id, widget_id, new_prefs):
try:
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
# The UPDATE query
query = """
UPDATE widgetPrefs
SET prefs = ?
WHERE client_id = ? AND widget_id = ?
"""
# Execute with parameters for security
cursor.execute(query, (new_prefs, client_id, widget_id))
# CRITICAL: Changes are not saved unless you commit
conn.commit()
if cursor.rowcount == 0:
print("No rows were updated. Check if the ID exists.")
else:
print(f"Successfully updated {widget_id} for {client_id}")
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
conn.close()
def addWidgetPref(widgetID, clientID, preferences):
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
query = "INSERT INTO widgetPrefs (client_id, widget_id, prefs) VALUES (?, ?, ?)"
data = (widgetID, clientID, preferences)
cursor.execute(query, data)
conn.commit()
conn.close()
def add_client(clientName, clientID):
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
query = "INSERT INTO clients (id, name) VALUES (?, ?)"
data = (clientID, clientName)
cursor.execute(query, data)
conn.commit()
conn.close()