-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoad_Student_View.py
173 lines (147 loc) · 5.21 KB
/
Load_Student_View.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
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
#Name: Vineet Dcunha
#"I have not given or received any unauthorized assistance on this assignment."
import csv
import sqlite3
import os
conn = sqlite3.connect('dsc450.db')
c = conn.cursor()
createstudenttbl = """
CREATE TABLE IF NOT EXISTS STUDENT (
STUDENTID NUMBER(10),
NAME VARCHAR2(50),
ADDRESS VARCHAR2(100),
GRADYEAR NUMBER(4),
CONSTRAINT STUDENT_PK PRIMARY KEY (STUDENTID)
);
"""
creategradetbl = """
CREATE TABLE IF NOT EXISTS GRADE (
CNAME VARCHAR2(100),
STUDENTID NUMBER(10),
CGRADE NUMBER(3,2),
CONSTRAINT GRADE_FK FOREIGN KEY (CNAME)
REFERENCES COURSE (CNAME),
CONSTRAINT GRADE_FK2 FOREIGN KEY (STUDENTID)
REFERENCES STUDENT (STUDENTID)
);
"""
createcoursetbl = """
CREATE TABLE IF NOT EXISTS COURSE (
CNAME VARCHAR2(100),
DEPARTMENT VARCHAR2(10),
CREDITS NUMBER(10),
CHAIR VARCHAR2(25),
CONSTRAINT COURSE_PK PRIMARY KEY (CNAME)
);
"""
createstudentdtlvw = """
CREATE VIEW IF NOT EXISTS STUDENT_DETAILS AS
SELECT
STU.STUDENTID,
STU.NAME,
STU.ADDRESS,
STU.GRADYEAR,
GR.CNAME,
GR.CGRADE,
CR.DEPARTMENT,
CR.CREDITS,
CR.CHAIR
FROM
STUDENT STU,
COURSE CR,
GRADE GR
WHERE
STU.STUDENTID = GR.STUDENTID
AND GR.CNAME = CR.CNAME;
"""
c.execute('DROP TABLE IF EXISTS STUDENT;')
c.execute(createstudenttbl) # create the STUDENT table
c.execute('DROP TABLE IF EXISTS COURSE;')
c.execute(createcoursetbl) # create the COURSE table
c.execute('DROP TABLE IF EXISTS GRADE;')
c.execute(creategradetbl) # create the GRADE table
c.execute('DROP VIEW IF EXISTS STUDENT_DETAILS;')
c.execute(createstudentdtlvw)
os.chdir("C:/Users/USER/Desktop/DSC/DSC_450 Database For Analytics/Midterm")
file_data = [i.strip('\n').split(',') for i in open('student.txt')]
new_data = [[int(a), *b, int(d)] for a, *b, d in file_data] #convert string id to integer and float
c.executemany('INSERT INTO STUDENT VALUES (?, ?, ?, ?)', new_data)
file_data = [i.strip('\n').split(',') for i in open('course.txt')]
new_data = [[*a, int(c), d] for *a, c, d in file_data] #convert string id to integer and float
c.executemany('INSERT INTO COURSE VALUES (?, ?, ?, ?)', new_data)
file_data = [i.strip('\n').split(',') for i in open('grade.txt')]
new_data = [[a, int(b), float(c)] for a, b, c in file_data] #convert string id to integer and float
c.executemany('INSERT INTO GRADE VALUES (?, ?, ?)', new_data)
result = c.execute('SELECT * FROM STUDENT_DETAILS')
print('Section a)')
translation = {39: None}
for row in result:
row = str(row)[1:-1]
print(row)
print('Fetching from view.')
result = c.execute('SELECT * FROM STUDENT_DETAILS')
translation = {39: None}
myfile = open('student_details.txt','w')
for row in result:
row = str(row)[1:-1]
myfile.write(row.translate(translation) + "\n")
print('\n','Section c)')
print('Adding the below record to the file.')
print("8, Elisa D Flores, 2548 Lakeshore Street California, 1994, Graphics, 3.5, Data Science, 2, Kevin Spacey",'\n')
myfile.write("8, Elisa D Flores, 2548 Lakeshore Street California, 1994, Graphics, 3.5, Data Science, 2, Kevin Spacey")
myfile.close()
print('Section d)')
fd = open('student_details.txt', 'r') # Read csv file
reader = csv.reader(fd)
unique = []
dupes = []
data = []
for row in reader:
col = row[4]+row[6]+row[7]
if col.strip() not in data:
data.append(col.strip())
for i in data:
subject,dept = i.split(' ',1)
rec = dept[:-2]
if rec not in unique:
unique.append(rec)
else:
dupes.append(rec)
if len(dupes) == 0:
print('Functional dependencies not violated. Every Course Name is unique.')
else:
print('Functional dependencies violated for the following course name. There are duplicate records for a combination of Credits and Course Name.')
print(dupes[0])
fd.close()
print('\n','Section e)')
result = c.execute('SELECT DEPARTMENT,ROUND(AVG(CGRADE),2),COUNT(1) FROM STUDENT_DETAILS GROUP BY DEPARTMENT')
translation = {39: None}
print('Department, Average Grade, Number of records')
for row in result:
row = str(row)[1:-1]
print(row.translate(translation))
file_data = [i.strip('\n').split(',') for i in open('student_details.txt')]
new_data = [[int(a),*b,int(d),e,float(f),g,int(h),i] for (a),*b,(d),e,(f),g,(h),i in file_data]
cleandata = []
for row in new_data:
col1 = row[5]
col2 = row[6]
cleandata.append([col2,float(col1)])
result = dict()
for k, v in cleandata:
if k in result:
result[k].append(v)
else:
result[k] = [v]
averages = []
for d in result:
averages.append((d, sum(result[d]) / len(result[d]), len(result[d])))
translation = {39: None}
print('\n','Section f)')
print('Department, Average Grade, Number of records')
for row in averages:
row = str(row)[1:-1]
print(row.translate(translation))
print('Note: Difference between the values from the txt file and SQL results (Data Science Department) is because of the extra record included in text file.')
conn.commit()
fd.close()