-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitialize_database.py
108 lines (94 loc) · 2.84 KB
/
initialize_database.py
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
# initialize_database.py
import sqlite3
def create_tables():
# Connect to the SQLite database (this will create a new database if it doesn't exist)
conn = sqlite3.connect('university.db')
# Create a cursor object to execute SQL queries
cursor = conn.cursor()
# Drop existing tables if they exist
cursor.execute('DROP TABLE IF EXISTS students')
cursor.execute('DROP TABLE IF EXISTS professors')
cursor.execute('DROP TABLE IF EXISTS reports')
cursor.execute('DROP TABLE IF EXISTS marks')
# Create a table for students
cursor.execute('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
password TEXT,
address TEXT,
phone TEXT,
mobile_phone TEXT,
nat_num TEXT,
public_key TEXT,
certificate TEXT
)
''')
# Create a table for professors
cursor.execute('''
CREATE TABLE IF NOT EXISTS professors (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
password TEXT,
address TEXT,
phone TEXT,
mobile_phone TEXT,
nat_num TEXT,
public_key TEXT,
certificate TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS reports (
id INTEGER PRIMARY KEY,
report TEXT,
student_id INTEGER,
FOREIGN KEY (student_id) REFERENCES students(id)
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS marks (
id INTEGER PRIMARY KEY,
marks TEXT,
professor_id INTEGER,
signature TEXT,
FOREIGN KEY (professor_id) REFERENCES professors(id)
)
''')
# Commit the changes
conn.commit()
# Close the cursor and connection
cursor.close()
conn.close()
def view_table(table_name):
conn = sqlite3.connect('university.db')
cursor = conn.cursor()
# Fetch all rows from the specified table
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
# Display the fetched data
for row in rows:
print(row)
# Close the cursor and connection
cursor.close()
conn.close()
if __name__ == "__main__":
x = input("enter: - (1) to create tables - (2) to view tables")
print(x)
if x == "1":
create_tables()
else:
print("STUDENTS:")
view_table("students")
print("-----------------------------")
print("REPORTS:")
view_table('reports')
print("-----------------------------")
print("PROFESSORS:")
view_table("professors")
print("-----------------------------")
print("MARKS:")
view_table('marks')
print("-----------------------------")