Skip to content

Commit

Permalink
refactored dbapi.py and added new data to booking.csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostas Gialantzis authored and Kostas Gialantzis committed Jul 10, 2022
1 parent 806c6f9 commit 6120cb1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 41 deletions.
15 changes: 0 additions & 15 deletions airline_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,3 @@ COPY duration FROM 'C:\database-class\data\duration.csv' DELIMITER ',' CSV HEADE
COPY ticket FROM 'C:\database-class\data\ticket.csv' DELIMITER ',' CSV HEADER; --9th --470 registers
COPY boarding_pass FROM 'C:\database-class\data\boarding_pass.csv' DELIMITER ',' CSV HEADER; --10th --470 registers

SELECT * FROM booking;
SELECT * FROM model;
SELECT * FROM aircraft;
SELECT * FROM airport;
SELECT * FROM passenger;
SELECT * FROM flight;
SELECT * FROM actual_status;
SELECT * FROM duration;
SELECT * FROM ticket;
SELECT * FROM boarding_pass;


--TO DOs
-- 1 --
--COPY commands for mac / eite na ta svisoume eite na valoume copy commands gia mac, eite na vroume kapoia universal ekdoxi kodika
4 changes: 2 additions & 2 deletions data/booking.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ book_ref,book_date,total_cost
000V66,6/16/2022 20:16,"$3,380.46 "
000S27,6/20/2022 15:08,"$8,082.92 "
000P04,6/29/2022 21:47,"$2,874.16 "
000Y18,7/8/2022 12:07,"$1,008.83 "
000Y18,7/9/2022 12:07,"$1,008.83 "
000F54,6/14/2022 19:08,$555.97
000J88,6/28/2022 12:57,"$6,021.79 "
000U93,6/8/2022 6:59,"$2,689.12 "
Expand All @@ -14,7 +14,7 @@ book_ref,book_date,total_cost
000X29,7/3/2022 2:46,"$3,124.30 "
000Y77,6/27/2022 10:44,"$2,517.80 "
000Q06,6/29/2022 15:38,"$1,971.94 "
000J34,6/16/2022 19:17,"$2,788.95 "
000J34,7/9/2022 19:17,"$2,788.95 "
000A99,6/20/2022 1:36,"$8,521.64 "
000U78,6/7/2022 10:22,"$8,770.46 "
000Y79,6/9/2022 17:25,$590.38
Expand Down
80 changes: 56 additions & 24 deletions dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@

import psycopg2


class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

def cursor(no_of_exercise):

# Query a
if(no_of_exercise == 'a' or no_of_exercise == 'A'):
print("a")
if(no_of_exercise == 'a' or no_of_exercise == 'A' or no_of_exercise == '1'):
print("-----------------------\n+++++" + bcolors.OKBLUE + "Query (a)" + bcolors.ENDC + "+++++\n-----------------------")
try:
#for every time i want to return a connection (conn) object
conn = psycopg2.connect(host = DB_HOST, dbname = DB_NAME, user = DB_USER, password = DB_PASSWORD)
Expand All @@ -19,28 +31,30 @@ def cursor(no_of_exercise):
cur = conn.cursor()

sqlQueryA = '''SELECT P.passenger_id, P.passenger_name, B.book_date
FROM flight AS F
JOIN ticket AS T ON F.flight_id = T.flight_id
JOIN booking AS B ON T.book_ref = B.book_ref
JOIN passenger AS P ON T.passenger_id = P.passenger_id
JOIN boarding_pass AS BP ON T.ticket_no = BP.ticket_no
WHERE F.flight_id = 33
AND BP.seat_no = '1A'
AND B.book_date = CURRENT_DATE - 1;'''
FROM flight AS F
JOIN ticket AS T ON F.flight_id = T.flight_id
JOIN booking AS B ON T.book_ref = B.book_ref
JOIN passenger AS P ON T.passenger_id = P.passenger_id
JOIN boarding_pass AS BP ON T.ticket_no = BP.ticket_no
WHERE F.flight_id = 33
AND BP.seat_no = '1A'
AND B.book_date = CURRENT_DATE - 1;'''

cur.execute(sqlQueryA)
results = cur.fetchall()
print(bcolors.BOLD + "passenger_id | passenger_name | book_date" + bcolors.ENDC)
for table in results:
print(table)
print("\n")
except:
print("Something gets wrong")
finally:
conn.close()
cur.close()

# Query B
elif(no_of_exercise == 'b' or no_of_exercise == 'B'):
print("b")
elif(no_of_exercise == 'b' or no_of_exercise == 'B') or no_of_exercise == '2':
print("-----------------------\n+++++" + bcolors.OKBLUE + "Query (b)" + bcolors.ENDC + "+++++\n-----------------------")
try:
#for every time i want to return a connection (conn) object
conn = psycopg2.connect(host = DB_HOST, dbname = DB_NAME, user = DB_USER, password = DB_PASSWORD)
Expand All @@ -59,17 +73,19 @@ def cursor(no_of_exercise):

cur.execute(sqlQueryB)
results = cur.fetchall()
print(bcolors.BOLD + "total_seats | reserved_seats | blank_seats" + bcolors.ENDC)
for table in results:
print(table)
print("\n")
except:
print("Something gets wrong")
finally:
conn.close()
cur.close()

# Query c
elif(no_of_exercise == 'c' or no_of_exercise == 'C'):
print("c")
elif(no_of_exercise == 'c' or no_of_exercise == 'C' or no_of_exercise == '3'):
print("-----------------------\n+++++" + bcolors.OKBLUE + "Query (c)" + bcolors.ENDC + "+++++\n-----------------------")
try:
#for every time i want to return a connection (conn) object
conn = psycopg2.connect(host = DB_HOST, dbname = DB_NAME, user = DB_USER, password = DB_PASSWORD)
Expand All @@ -89,17 +105,19 @@ def cursor(no_of_exercise):

cur.execute(sqlQueryC)
results = cur.fetchall()
print(bcolors.BOLD + "flight_id | delay_in_hours | departure_date" + bcolors.ENDC)
for table in results:
print(table)
print("\n")
except:
print("Something gets wrong")
finally:
conn.close()
cur.close()

# Query d
elif(no_of_exercise == 'd' or no_of_exercise == 'D'):
print("d")
elif(no_of_exercise == 'd' or no_of_exercise == 'D' or no_of_exercise == '4'):
print("-----------------------\n+++++" + bcolors.OKBLUE + "Query (d)" + bcolors.ENDC + "+++++\n-----------------------")
try:
#for every time i want to return a connection (conn) object
conn = psycopg2.connect(host = DB_HOST, dbname = DB_NAME, user = DB_USER, password = DB_PASSWORD)
Expand All @@ -119,17 +137,19 @@ def cursor(no_of_exercise):

cur.execute(sqlQueryD)
results = cur.fetchall()
print(bcolors.BOLD + "passenger_id | passenger_name | total_km_flown" + bcolors.ENDC)
for table in results:
print(table)
print("\n")
except:
print("Something gets wrong")
finally:
conn.close()
cur.close()

# Query e
elif(no_of_exercise == 'e' or no_of_exercise == 'E'):
print("e")
elif(no_of_exercise == 'e' or no_of_exercise == 'E' or no_of_exercise == '5'):
print("-----------------------\n+++++" + bcolors.OKBLUE + "Query (e)" + bcolors.ENDC + "+++++\n-----------------------")
try:
#for every time i want to return a connection (conn) object
conn = psycopg2.connect(host = DB_HOST, dbname = DB_NAME, user = DB_USER, password = DB_PASSWORD)
Expand All @@ -148,17 +168,19 @@ def cursor(no_of_exercise):

cur.execute(sqlQueryE)
results = cur.fetchall()
print(bcolors.BOLD + "city | no_of_appears" + bcolors.ENDC)
for table in results:
print(table)
print("\n")
except:
print("Something gets wrong")
finally:
conn.close()
cur.close()

# Query f
elif(no_of_exercise == 'f' or no_of_exercise == 'F'):
print("f")
elif(no_of_exercise == 'f' or no_of_exercise == 'F' or no_of_exercise == '6'):
print("-----------------------\n+++++" + bcolors.OKBLUE + "Query (f)" + bcolors.ENDC + "+++++\n-----------------------")
try:
#for every time i want to return a connection (conn) object
conn = psycopg2.connect(host = DB_HOST, dbname = DB_NAME, user = DB_USER, password = DB_PASSWORD)
Expand All @@ -181,16 +203,26 @@ def cursor(no_of_exercise):

cur.execute(sqlQueryF)
results = cur.fetchall()
print(bcolors.BOLD + "passenger_id | passenger_name | passengers_total_flights || average_boarding_que" + bcolors.ENDC)
for table in results:
print(table)
print("\n")
except:
print("Something gets wrong")
finally:
conn.close()
cur.close()
else:
if(no_of_exercise == 'q'):
return
else:
print(bcolors.RED + "Give a right Number of an exercise (for ex. type 'a' or '1' for the 1st query)!" + bcolors.ENDC)

while True:
str = input("Grapse pio erwtima thes na ulopoihseis 'h pata 'q' gia quit: ")
cursor(str)
if(str == 'q'):
break
try:
str = input("Give the Number of the exercise you want to show (or type 'q' to quit):\n ")
cursor(str)
if(str == 'q'):
break
except:
print(bcolors.RED + "something wrong" + bcolors.ENDC)

0 comments on commit 6120cb1

Please sign in to comment.