-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
123 lines (104 loc) · 4.12 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
import sqlite3
def create_connection():
"""Create a database connection to the SQLite database."""
return sqlite3.connect('properties.db')
def create_tables():
"""Create the necessary tables."""
conn = create_connection()
cursor = conn.cursor()
# iniiate properties table
cursor.execute('''
CREATE TABLE IF NOT EXISTS properties (
id INTEGER PRIMARY KEY AUTOINCREMENT,
address TEXT UNIQUE NOT NULL,
asking_price REAL NOT NULL,
bedrooms INTEGER NOT NULL,
category TEXT NOT NULL,
market_date TEXT NOT NULL,
vendor TEXT NOT NULL,
photo_path TEXT,
video_path TEXT
);
''')
# implemented appointments table, that stores appointment details.
cursor.execute('''
CREATE TABLE IF NOT EXISTS appointments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
property_address TEXT NOT NULL, -- Changed from property_id to property_address
time TEXT NOT NULL,
viewer_name TEXT NOT NULL,
contact_number TEXT NOT NULL,
email_address TEXT NOT NULL,
notes TEXT
);
''')
conn.commit()
conn.close()
#function to store new appoitnments into appointment table
def initiateAppointment(date, property_address, time, viewer_name, contact_number, email_address, notes):
"""Insert a new appointment into the appointments table."""
conn = create_connection()
cursor = conn.cursor()
try:
cursor.execute('''
INSERT INTO appointments (date, property_address, time, viewer_name, contact_number, email_address, notes)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (date, property_address, time, viewer_name, contact_number, email_address, notes))
conn.commit()
return True
except sqlite3.Error as e:
print(f"An error occurred while inserting appointment: {e}")
return False
finally:
conn.close()
#function to filter appointments based on date/time, property address etc.
def filterAppointments(date=None, property_address=None, time=None):
"""Fetch appointments from the appointments table."""
conn = create_connection()
cursor = conn.cursor()
query = "SELECT * FROM appointments WHERE 1=1"
params = []
if date:
query += " AND date = ?"#filter appointment by date
params.append(date)
if property_address:
query += " AND property_address = ?"#filter appointment by property
params.append(property_address)
if time:
query += " AND time = ?"#filter appointment by time
params.append(time)
cursor.execute(query, params)
appointments = cursor.fetchall()
conn.close()
return appointments
def deleteAppointment(appointment_id):
"""Delete an appointment from the appointments table."""
conn = create_connection()
cursor = conn.cursor()
try:
cursor.execute("DELETE FROM appointments WHERE id=?", (appointment_id,))
conn.commit()
return True
except sqlite3.Error as e:
print(f"An error occurred while deleting appointment: {e}")
return False
finally:
conn.close()
def update_appointment(appointment_id, date, property_address, time, viewer_name, contact_number, email_address, notes):
"""Update an existing appointment in the appointments table."""
conn = create_connection()
cursor = conn.cursor()
try:
cursor.execute('''
UPDATE appointments
SET date = ?, property_address = ?, time = ?, viewer_name = ?, contact_number = ?, email_address = ?, notes = ?
WHERE id = ?
''', (date, property_address, time, viewer_name, contact_number, email_address, notes, appointment_id))
conn.commit()
return True
except sqlite3.Error as e:
print(f"An error occurred while updating appointment: {e}")
return False
finally:
conn.close()