-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
64 lines (60 loc) · 2.06 KB
/
Copy pathdatabase.py
File metadata and controls
64 lines (60 loc) · 2.06 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
import mysql.connector
from mysql.connector import Error
def create_connection():
"""Create a database connection to the MySQL database."""
try:
connection = mysql.connector.connect(
host='localhost',
database='study_planner_db',
user='root',
password='@Awais123a',
port=3306
)
if connection.is_connected():
return connection
except Error as e:
print(f"Error while connecting to MySQL: {e}")
return None
def create_tables(connection):
"""Create the necessary tables in the database."""
cursor = connection.cursor()
try:
cursor.execute("""
CREATE TABLE IF NOT EXISTS Students (
ID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Email VARCHAR(255) UNIQUE NOT NULL,
Password VARCHAR(255) NOT NULL
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS Subjects (
Subject_ID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Difficulty_Level VARCHAR(50),
Student_ID INT,
FOREIGN KEY (Student_ID) REFERENCES Students(ID)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS Study_History (
Record_ID INT AUTO_INCREMENT PRIMARY KEY,
Student_ID INT,
Subject_ID INT,
Date DATE NOT NULL,
Hours_Studied FLOAT NOT NULL,
FOREIGN KEY (Student_ID) REFERENCES Students(ID),
FOREIGN KEY (Subject_ID) REFERENCES Subjects(Subject_ID)
);
""")
print("Tables created successfully.")
except Error as e:
print(f"Error creating tables: {e}")
finally:
cursor.close()
if __name__ == '__main__':
conn = create_connection()
if conn:
create_tables(conn)
conn.close()
print("Database setup is complete.")