-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_handling.py
More file actions
88 lines (74 loc) · 2.37 KB
/
db_handling.py
File metadata and controls
88 lines (74 loc) · 2.37 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 time
def initialiseTable():
db_path = "cars.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Create table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS vehicle_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timeEntered TEXT,
ownerType TEXT,
vehicleOwner TEXT,
vehicleNumber TEXT,
entryAllowed TEXT
)
''')
def writeToSQL(owner, number, ownerType, entryAllowed):
db_path = "cars.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Create table if it doesn't exist
initialiseTable()
# Insert new data
new_data = (time.strftime("%H:%M", time.localtime(time.time())), ownerType, owner, number, entryAllowed)
print("Writing the following:", new_data)
cursor.execute('''
INSERT INTO vehicle_entries (timeEntered, ownerType, vehicleOwner, vehicleNumber, entryAllowed)
VALUES (?, ?, ?, ?, ?)
''', new_data)
print("wrote")
# Commit changes and close connection
conn.commit()
conn.close()
def readSQL():
db_path = "savedcars.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Query the vehicle_entries table
cursor.execute('SELECT vehicleNumber, vehicleOwner, ownerType FROM saved_cars')
rows = cursor.fetchall()
# Convert the rows to a list of dictionaries
data = []
for row in rows:
entry = {
"vehicleNumber": row[0],
"vehicleOwner": row[1],
"ownerType": row[2]
}
data.append(entry)
# Close the connection
conn.close()
return data
def registerCar(vehicleNumber, vehicleOwner, ownerType):
db_path = "savedcars.db"
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Create table if it doesn't exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS saved_cars (
vehicleNumber TEXT PRIMARY KEY,
vehicleOwner TEXT,
ownerType TEXT
)
''')
# Insert new data
new_data = (vehicleNumber, vehicleOwner, ownerType)
cursor.execute('''
INSERT INTO saved_cars (vehicleNumber, vehicleOwner, ownerType)
VALUES (?, ?, ?)
''', new_data)
# Commit changes and close connection
conn.commit()
conn.close()