-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
106 lines (96 loc) · 3.05 KB
/
Copy pathdatabase.py
File metadata and controls
106 lines (96 loc) · 3.05 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
import sqlite3
from sqlite3 import Error
DATABASE_NAME = "flights.db"
def create_connection():
conn = None
try:
conn = sqlite3.connect(DATABASE_NAME)
return conn
except Error as e:
print(f"Error connecting to database: {e}")
return conn
def create_table(conn):
try:
sql_create_reservations_table = """
CREATE TABLE IF NOT EXISTS reservations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
flight_number TEXT NOT NULL,
departure TEXT NOT NULL,
destination TEXT NOT NULL,
date TEXT NOT NULL,
seat_number TEXT NOT NULL
);
"""
cursor = conn.cursor()
cursor.execute(sql_create_reservations_table)
conn.commit()
except Error as e:
print(f"Error creating table: {e}")
def add_reservation(conn, reservation_details):
sql = ''' INSERT INTO reservations(name, flight_number, departure, destination, date, seat_number)
VALUES(?,?,?,?,?,?) '''
cursor = conn.cursor()
try:
cursor.execute(sql, reservation_details)
conn.commit()
return cursor.lastrowid
except Error as e:
print(f"Error adding reservation: {e}")
return None
def get_all_reservations(conn):
cursor = conn.cursor()
try:
cursor.execute("SELECT * FROM reservations ORDER BY id DESC")
rows = cursor.fetchall()
return rows
except Error as e:
print(f"Error fetching reservations: {e}")
return []
def get_reservation_by_id(conn, reservation_id):
cursor = conn.cursor()
try:
cursor.execute("SELECT * FROM reservations WHERE id=?", (reservation_id,))
row = cursor.fetchone()
return row
except Error as e:
print(f"Error fetching reservation by ID: {e}")
return None
def update_reservation(conn, reservation_id, updated_details):
sql = ''' UPDATE reservations
SET name = ? ,
flight_number = ? ,
departure = ? ,
destination = ? ,
date = ? ,
seat_number = ?
WHERE id = ?'''
cursor = conn.cursor()
try:
data_to_update = updated_details + (reservation_id,)
cursor.execute(sql, data_to_update)
conn.commit()
return True
except Error as e:
print(f"Error updating reservation: {e}")
return False
def delete_reservation(conn, reservation_id):
sql = 'DELETE FROM reservations WHERE id=?'
cursor = conn.cursor()
try:
cursor.execute(sql, (reservation_id,))
conn.commit()
return True
except Error as e:
print(f"Error deleting reservation: {e}")
return False
def initialize_database():
conn = create_connection()
if conn is not None:
create_table(conn)
conn.close()
else:
print("Error! Cannot create the database connection.")
if __name__ == '__main__':
initialize_database()
print(f"Database '{DATABASE_NAME}' initialized.")