-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
186 lines (159 loc) · 5.29 KB
/
Copy pathsql.py
File metadata and controls
186 lines (159 loc) · 5.29 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
# import mysql.connector
from dotenv import load_dotenv
import sqlite3
import os
load_dotenv()
# # Connect to the Mysql Database
# connection = mysql.connector.connect(
# host=os.getenv("MYSQL_HOST"),
# user=os.getenv("MYSQL_USER"),
# password=os.getenv("MYSQL_PASSWORD"),
# database=os.getenv("MYSQL_DATABASE")
# )
db_path = os.path.join(os.getcwd(), 'sample.db')
connection = sqlite3.connect(db_path)
# Cursor for executing queries
cursor = connection.cursor()
# Drop tables if they already exist
cursor.execute("DROP TABLE IF EXISTS students")
cursor.execute("DROP TABLE IF EXISTS courses")
cursor.execute("DROP TABLE IF EXISTS departments")
cursor.execute("DROP TABLE IF EXISTS enrollments")
cursor.execute("DROP TABLE IF EXISTS payments")
# # Create table departments
# departments_table = """
# CREATE TABLE departments (
# department_id INT PRIMARY KEY,
# department_name VARCHAR(50)
# );
# """
departments_table = """
CREATE TABLE departments (
department_id INTEGER PRIMARY KEY,
department_name TEXT
);
"""
cursor.execute(departments_table)
# # Create table courses
# courses_table = """
# CREATE TABLE courses (
# course_id INT PRIMARY KEY,
# course_name VARCHAR(50),
# department_id INT,
# FOREIGN KEY(department_id) REFERENCES departments(department_id)
# );
# """
courses_table = """
CREATE TABLE courses (
course_id INTEGER PRIMARY KEY,
course_name TEXT,
department_id INTEGER,
FOREIGN KEY(department_id) REFERENCES departments(department_id)
);
"""
cursor.execute(courses_table)
# # Create table students
# students_table = """
# CREATE TABLE students (
# student_id INT PRIMARY KEY,
# name VARCHAR(25),
# department_id INT,
# batch INT,
# section VARCHAR(25),
# course_id INT,
# marks INT,
# FOREIGN KEY(department_id) REFERENCES departments(department_id),
# FOREIGN KEY(course_id) REFERENCES courses(course_id)
# );
# """
students_table = """
CREATE TABLE students (
student_id INTEGER PRIMARY KEY,
name TEXT,
department_id INTEGER,
batch INTEGER,
section TEXT,
course_id INTEGER,
marks INTEGER,
FOREIGN KEY(department_id) REFERENCES departments(department_id),
FOREIGN KEY(course_id) REFERENCES courses(course_id)
);
"""
cursor.execute(students_table)
# # Create table enrollments
# enrollments_table = """
# CREATE TABLE enrollments (
# enrollment_id INT PRIMARY KEY,
# student_id INT,
# course_id INT,
# enrollment_date DATE,
# FOREIGN KEY(student_id) REFERENCES students(student_id),
# FOREIGN KEY(course_id) REFERENCES courses(course_id)
# );
# """
enrollments_table = """
CREATE TABLE enrollments (
enrollment_id INTEGER PRIMARY KEY,
student_id INTEGER,
course_id INTEGER,
enrollment_date TEXT,
FOREIGN KEY(student_id) REFERENCES students(student_id),
FOREIGN KEY(course_id) REFERENCES courses(course_id)
);
"""
cursor.execute(enrollments_table)
# # Create table payments
# payments_table = """
# CREATE TABLE payments (
# payment_id INT PRIMARY KEY,
# student_id INT,
# amount DECIMAL(10, 2),
# payment_date DATE,
# FOREIGN KEY(student_id) REFERENCES students(student_id)
# );
# """
payments_table = """
CREATE TABLE payments (
payment_id INTEGER PRIMARY KEY,
student_id INTEGER,
amount DECIMAL(10, 2),
payment_date TEXT,
FOREIGN KEY(student_id) REFERENCES students(student_id)
);
"""
cursor.execute(payments_table)
# data for departments
cursor.execute("INSERT INTO departments VALUES (1, 'Computer Science')")
cursor.execute("INSERT INTO departments VALUES (2, 'Electrical Engineering')")
cursor.execute("INSERT INTO departments VALUES (3, 'Mechanical Engineering')")
# data for courses
cursor.execute("INSERT INTO courses VALUES (1, 'Artificial Intelligence', 1)")
cursor.execute("INSERT INTO courses VALUES (2, 'Machine Learning', 1)")
cursor.execute("INSERT INTO courses VALUES (3, 'Power Systems', 2)")
# data for students
cursor.execute("INSERT INTO students VALUES (235, 'Alice Johnson', 1, 9, 'A', 1, 80)")
cursor.execute("INSERT INTO students VALUES (236, 'Bob Smith', 2, 9, 'A', 3, 88)")
cursor.execute("INSERT INTO students VALUES (237, 'Charlie Brown', 1, 9, 'A', 2, 72)")
cursor.execute("INSERT INTO students VALUES (238, 'Diana Prince', 2, 9, 'A', 3, 90)")
cursor.execute("INSERT INTO students VALUES (239, 'Rakibul Islam', 1, 9, 'A', 1, 85)")
# data for enrollments
cursor.execute("INSERT INTO enrollments VALUES (101, 235, 1, '2024-09-01')")
cursor.execute("INSERT INTO enrollments VALUES (102, 236, 3, '2024-09-02')")
cursor.execute("INSERT INTO enrollments VALUES (103, 237, 2, '2024-09-03')")
cursor.execute("INSERT INTO enrollments VALUES (104, 238, 3, '2024-09-04')")
cursor.execute("INSERT INTO enrollments VALUES (105, 239, 1, '2024-09-05')")
# data for payments
cursor.execute("INSERT INTO payments VALUES (201, 235, 1000.00, '2024-09-01')")
cursor.execute("INSERT INTO payments VALUES (202, 236, 1500.00, '2024-09-02')")
cursor.execute("INSERT INTO payments VALUES (203, 237, 1200.00, '2024-09-03')")
# Commit the changes
connection.commit()
# # Fetch and display the data from the table
# cursor.execute("SELECT * FROM students")
# rows = cursor.fetchall()
# print("Inserted Records in Students Table:")
# for row in rows:
# print(row)
# Close the cursor and connection
cursor.close()
connection.close()