def normalizedatabase(nonnormalizeddbfilename): connnormalized =…
- User
def normalize_database(non_normalized_db_filename):
conn_normalized = create_connection('normalized.db', True) create_table_Degree = """CREATE TABLE IF NOT EXISTS [Degrees](Degree TEXT NOT NULL PRIMARY KEY);""" create_table(conn_normalized, create_table_Degree) create_table_Exam = """CREATE TABLE IF NOT EXISTS [Exams](Exam TEXT NOT NULL PRIMARY KEY, Year INT NOT NULL);""" create_table(conn_normalized, create_table_Exam) create_table_Students = """CREATE TABLE IF NOT EXISTS [Students](StudentID INTEGER NOT NULL PRIMARY KEY, First_Name TEXT NOT NULL , Last_Name TEXT NOT NULL,Degree TEXT NOT NULL, FOREIGN KEY(Degree) REFERENCES Degrees(Degree));""" create_table(conn_normalized, create_table_Students) create_table_StudentsExamScores = """CREATE TABLE IF NOT EXISTS [StudentsExamScores](PK INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT ,StudentID INTEGER NOT NULL, Exam TEXT NOT NULL, Score INTEGER NOT NULL,FOREIGN KEY(StudentID) REFERENCES Students(StudentID), FOREIGN KEY(Exam) REFERENCES Exams(Exam));""" create_table(conn_normalized, create_table_StudentsExamScores) conn_non_normalized = create_connection(non_normalized_db_filename , False) select_degree = '''SELECT DISTINCT Degree FROM Students''' select_exams = ''' SELECT Exams FROM Students''' select_students = '''SELECT StudentID,Name,Degree From Students''' students = execute_sql_statement(select_students,conn_non_normalized)print(students)
stu = [(id, name.split(',')[0].strip(), name.split(',')[1].strip(), degree) for id, name, degree in students] st = tuple(stu)print(st)
slect_s = '''SELECT Scores FROM Students''' scores = execute_sql_statement(slect_s,conn_non_normalized) slect_id = '''SELECT StudentID FROM Students''' sid = execute_sql_statement(slect_id,conn_non_normalized)print(sid)
sids = [] for sco in sid: for i in sco: sids.append(i)print(sids)
print(scores)
scor = [] for sco in scores: for i in sco: scor.append(i.split(","))print(scor)
c = [] for sublist in scor: new_sublist = [] for item in sublist: new_sublist.append(int(item.strip())) c.append(new_sublist)print(c)
degrees = execute_sql_statement(select_degree,conn_non_normalized)print(degrees)
tup = [] for i in degrees: for j in i: tup.append(j) deg = tuple(tup) exams = execute_sql_statement(select_exams ,conn_non_normalized)print(exams)
k =[] for i in exams: for j in i: x = j.split(',') k.append(x) m = [] for i in k: for j in i: if j not in m: m.append(j) o = [(s.split()[0], int(s.split()[1].strip('()'))) for s in m] ex = [] for j in k: lo = [] for l in j: lo.append(l.split('(')[0].strip()) ex.append(lo) - Assistant
- User
- Assistant
It looks like the error is being caused because the
Studentstable in the non-normalized database doesn't have a column namedExam. You can try updating the query to useExamsinstead ofStudentslike this:exams = set((row[0], int(row[1])) for row in execute_sql_statement('''SELECT DISTINCT Exam, Year FROM Exams''', conn_non_normalized))Additionally, it seems that the method causing the error is not
normalize_databasebut rather thesetUpClassmethod intest_assignment.py. You might want to take a look at that method to see if you're correctly setting up the test environment. - User
def ex30(conn): # Write an SQL statement that SELECTs all rows from the
Examstable and sort the exams by Year # output columns: exam, year### BEGIN SOLUTION sql_statement = "" ### END SOLUTION # df = pd.read_sql_query(sql_statement, conn) # display(df) return sql_statement - Assistant
You can use the following SQL statement to select all rows from the
Examstable and sort by year:SELECT exam, year FROM Exams ORDER BY year;Here's the updated function:
def ex30(conn): # Write an SQL statement that SELECTs all rows from the `Exams` table and sort the exams by Year # output columns: exam, year sql_statement = "SELECT exam, year FROM Exams ORDER BY year;" # df = pd.read_sql_query(sql_statement, conn) # display(df) return sql_statementThis function simply returns the SQL statement as a string. If you uncomment the commented code and run the function, it should display the result of executing the SQL statement as a pandas dataframe.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.