-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_db.py
More file actions
81 lines (73 loc) · 2.3 KB
/
create_db.py
File metadata and controls
81 lines (73 loc) · 2.3 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
# create_db.py
import sqlite3
def create_database():
conn = sqlite3.connect('database/movie_ticket.db')
cursor = conn.cursor()
# Users table with better security
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
is_admin BOOLEAN DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Movies table with more details
cursor.execute('''
CREATE TABLE IF NOT EXISTS movies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT,
duration INTEGER,
genre TEXT,
release_date DATE,
poster_url TEXT,
is_active BOOLEAN DEFAULT 1
)
''')
# Showtimes table
cursor.execute('''
CREATE TABLE IF NOT EXISTS showtimes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
movie_id INTEGER NOT NULL,
showtime DATETIME NOT NULL,
theater_name TEXT NOT NULL,
available_seats INTEGER NOT NULL,
price DECIMAL(5,2) NOT NULL,
FOREIGN KEY (movie_id) REFERENCES movies (id)
)
''')
# Bookings table
cursor.execute('''
CREATE TABLE IF NOT EXISTS bookings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
showtime_id INTEGER NOT NULL,
num_tickets INTEGER NOT NULL,
total_price DECIMAL(6,2) NOT NULL,
booking_date DATETIME DEFAULT CURRENT_TIMESTAMP,
status TEXT DEFAULT 'confirmed',
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (showtime_id) REFERENCES showtimes (id)
)
''')
# Payments table
cursor.execute('''
CREATE TABLE IF NOT EXISTS payments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
booking_id INTEGER NOT NULL,
amount DECIMAL(6,2) NOT NULL,
payment_method TEXT NOT NULL,
payment_status TEXT DEFAULT 'pending',
transaction_id TEXT,
payment_date DATETIME,
FOREIGN KEY (booking_id) REFERENCES bookings (id)
)
''')
conn.commit()
conn.close()
print("Database created successfully with improved schema!")
if __name__ == '__main__':
create_database()