Repository navigation
Expand file tree
/
Copy pathDatabase.py
More file actions
291 lines (248 loc) · 9.19 KB
/
Copy pathDatabase.py
File metadata and controls
291 lines (248 loc) · 9.19 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import sqlite3
import pandas as pd
import os
class Database:
def __init__(self, db_name):
"""
Initialize Database class.
Establishes a SQLite connection and creates a cursor.
"""
self.conn = sqlite3.connect(db_name)
self.c = self.conn.cursor()
def create_database(self):
"""
Creates tables in the database for airports, airlines, and flights.
"""
c = self.conn.cursor()
# self.conn.execute("PRAGMA foreign_keys = 1")
c.execute('''
DROP TABLE IF EXISTS airports;
''')
c.execute('''
CREATE TABLE airports (
id INTEGER PRIMARY KEY,
code TEXT,
city TEXT NOT NULL,
country TEXT NOT NULL
);
''')
c.execute('''
DROP TABLE IF EXISTS airlines;
''')
c.execute('''
CREATE TABLE airlines (
id INTEGER PRIMARY KEY,
code TEXT,
name TEXT NOT NULL
);
''')
c.execute('''
DROP TABLE IF EXISTS flights;
''')
c.execute('''
CREATE TABLE flights (
id INTEGER PRIMARY KEY,
date TEXT NOT NULL,
airline TEXT,
origin TEXT,
destination TEXT,
departure_time INTEGER,
arrival_time INTEGER,
flight_time INTEGER,
distance DECIMAL,
FOREIGN KEY (airline) REFERENCES airlines(code),
FOREIGN KEY (origin) REFERENCES airports(code),
FOREIGN KEY (destination) REFERENCES airports(code)
);
''')
self.conn.commit()
def load_csv_data(self):
"""
Loads data from CSV files into the airports, airlines, and flights tables.
"""
conn = self.conn
if os.path.exists('data/airports.csv'):
try:
airports = pd.read_csv('data/airports.csv')
airports.to_sql('airports', conn, if_exists='append', index=False)
except pd.errors.ParserError:
print('Error reading airports.csv')
else:
print('airports.csv not found')
if os.path.exists('data/airlines.csv'):
try:
airlines = pd.read_csv('data/airlines.csv')
airlines.to_sql('airlines', conn, if_exists='append', index=False)
except pd.errors.ParserError:
print('Error reading airlines.csv')
else:
print('airlines.csv not found')
if os.path.exists('data/flights.csv'):
try:
flights = pd.read_csv('data/flights.csv')
flights.to_sql('flights', conn, if_exists='append', index=False)
except pd.errors.ParserError:
print('Error reading flights.csv')
else:
print('flights.csv not found')
self.conn.commit()
def load_list_airports(self, airport_values):
"""
Inserts the given airport values into the airports table.
"""
for i, airport in enumerate(airport_values):
self.c.execute(
f'''
INSERT INTO airports (id, code, city, country)
VALUES ({i}, '{airport[0]}', '{airport[1]}', '{airport[2]}');
''')
self.conn.commit()
def load_list_airlines(self, airline_values):
"""
Inserts the given airline values into the airlines table.
"""
for i, airline in enumerate(airline_values):
self.c.execute(
f'''
INSERT INTO airlines (id, code, name)
VALUES ({i}, '{airline[0]}', '{airline[1]}');
''')
self.conn.commit()
def load_list_flights(self, flights_values):
"""
Inserts the given flight values into the flights table.
"""
for i, flight in enumerate(flights_values):
self.c.execute(
f'''
INSERT INTO flights
(id, date, airline, origin, destination, departure_time, arrival_time, flight_time, distance)
VALUES ({i}, '{flight[0]}', '{flight[1]}', '{flight[2]}', '{flight[3]}', {flight[4]},
{flight[5]}, {flight[6]}, {flight[7]});
''')
self.conn.commit()
def get_flights_by_airline(self, airline_code):
"""
Returns all flights by the given airline code.
"""
c = self.conn.cursor()
c.execute('''
SELECT *
FROM flights
INNER JOIN airlines ON flights.airline = airlines.code
WHERE airlines.code = ?
''', (airline_code,))
return c.fetchall()
def get_flights_between_airports(self, origin_code, destination_code):
"""
Returns all flights between the given origin and destination airports.
"""
c = self.conn.cursor()
c.execute('''
SELECT *
FROM flights
INNER JOIN airports AS origin_airports ON flights.origin = origin_airports.code
INNER JOIN airports AS destination_airports ON flights.destination = destination_airports.code
WHERE origin_airports.code = ? AND destination_airports.code = ?
''', (origin_code, destination_code))
return c.fetchall()
def update_airports(self, id, code, city, country):
"""
Updates an airport with the given id.
"""
self.c.execute('''
UPDATE airports
SET code = ?, city = ?, country = ?
WHERE id = ?
''', (code, city, country, id))
self.conn.commit()
def update_airlines(self, id, code, name):
"""
Updates an airline with the given id.
"""
self.c.execute('''
UPDATE airlines
SET code = ?, name = ?
WHERE id = ?
''', (code, name, id))
self.conn.commit()
def update_flights(self, id, date, airline, origin, destination, departure_time, arrival_time, flight_time,
distance):
"""
Updates a flight with the given id.
"""
self.c.execute('''
UPDATE flights
SET date = ?, airline = ?, origin = ?, destination = ?, departure_time = ?, arrival_time = ?, flight_time = ?, distance = ?
WHERE id = ?
''', (date, airline, origin, destination, departure_time, arrival_time, flight_time, distance, id))
self.conn.commit()
def delete_airports(self, id_start, id_end):
"""
Deletes airports with ids in the given range.
"""
self.c.execute('DELETE FROM airports WHERE id BETWEEN ? AND ?', (id_start, id_end))
self.conn.commit()
def delete_airlines(self, id_start, id_end):
"""
Deletes airlines with ids in the given range.
"""
self.c.execute('DELETE FROM airlines WHERE id BETWEEN ? AND ?', (id_start, id_end))
self.conn.commit()
def delete_flights(self, id_start, id_end):
"""
Deletes flights with ids in the given range.
"""
self.c.execute('DELETE FROM flights WHERE id BETWEEN ? AND ?', (id_start, id_end))
self.conn.commit()
def add_airport(self, code, name, city, country):
self.cursor.execute("""
INSERT INTO airports (code, name, city, country)
VALUES (?, ?, ?, ?)
""", (code, name, city, country))
self.conn.commit()
def add_airline(self, code, name, country):
self.cursor.execute("""
INSERT INTO airlines (code, name, country)
VALUES (?, ?, ?)
""", (code, name, country))
self.conn.commit()
def add_flight(self, airline_id, origin_id, destination_id):
self.cursor.execute("""
INSERT INTO flights (airline_id, origin_id, destination_id)
VALUES (?, ?, ?)
""", (airline_id, origin_id, destination_id))
self.conn.commit()
def get_airport(self, airport_id):
self.cursor.execute("""
SELECT * FROM airports WHERE id = ?
""", (airport_id,))
return self.cursor.fetchone()
def get_airline(self, airline_id):
self.cursor.execute("""
SELECT * FROM airlines WHERE id = ?
""", (airline_id,))
return self.cursor.fetchone()
def get_flight(self, flight_id):
self.cursor.execute("""
SELECT * FROM flights WHERE id = ?
""", (flight_id,))
return self.cursor.fetchone()
def drop_db(self, password):
"""
Drops the database if the correct password is provided.
"""
if password == 'your_password':
self.c.execute('''
DROP TABLE IF EXISTS airports;
''')
self.c.execute('''
DROP TABLE IF EXISTS airlines;
''')
self.c.execute('''
DROP TABLE IF EXISTS flights;
''')
self.conn.commit()
print("Database dropped successfully.")
else:
print("Incorrect password. Database was not dropped.")