connnormalized = createconnection('normalized.db', True) createtable(connnormalized,…
- User
conn_normalized = create_connection('normalized.db', True) create_table(conn_normalized, """CREATE TABLE IF NOT EXISTS [Degrees](Degree TEXT NOT NULL PRIMARY KEY);""") create_table(conn_normalized, """CREATE TABLE IF NOT EXISTS [Exams](Exam TEXT NOT NULL PRIMARY KEY, Year INT NOT NULL);""") create_table(conn_normalized, """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 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));""")
# fetch data from non-normalized database conn_non_normalized = create_connection(non_normalized_db_filename , False) degrees = set(row[0] for row in execute_sql_statement('''SELECT DISTINCT Degree FROM Students''', conn_non_normalized)) exams = set((row[0], int(row[1])) for row in execute_sql_statement("""SELECT DISTINCT Exam, Year FROM Exams""", conn_non_normalized)) students = [(id, first.strip(), last.strip(), degree) for id, name, degree in execute_sql_statement('''SELECT StudentID, Name, Degree FROM Students''', conn_non_normalized) for first, last in [name.split(',')]] scores = [s.strip().split(',') for row in execute_sql_statement('''SELECT Scores FROM Students''', conn_non_normalized) for s in row] students_scores = [(student_id, exam, int(score)) for student_id, student_scores in zip([row[0] for row in students], scores) for exam, score in zip([exam[0] for exam in exams], student_scores)] # insert data into normalized database with conn_normalized: [insert_degrees(conn_normalized, (degree,)) for degree in degrees] [insert_exams(conn_normalized, exam) for exam in exams] [insert_students(conn_normalized, student) for student in students] [insert_studentexamscores(conn_normalized, student_score) for student_score in students_scores] def insert_degrees(conn_normalized, values): sql = '''INSERT INTO Degrees(Degree) VALUES(?)''' cur = conn_normalized.cursor() cur.execute(sql, values) return cur.lastrowid def insert_exams(conn_normalized, values): sql = '''INSERT INTO Exams(Exam, Year) VALUES(?, ?)''' cur = conn_normalized.cursor() cur.execute(sql, values) return cur.lastrowid def insert_students(conn_normalized, values): sql = '''INSERT INTO Students(StudentID, First_Name, Last_Name, Degree) VALUES (?, ?, ?, ?)''' cur = conn_normalized.cursor() cur.execute(sql, values) return cur.lastrowid def insert_studentexamscores(conn_normalized, values): sql = '''INSERT INTO StudentsExamScores(StudentID, Exam, Score) VALUES (?, ?, ?)''' cur = conn_normalized.cursor() cur.execute(sql, values) return cur.lastrowidthe above code is giving following error: Traceback (most recent call last): File "C:\Users\Tejesh Pawar\AppData\Local\Programs\Python\Python311\Lib\unittest\suite.py", line 166, in _handleClassSetUp setUpClass() File "C:\Users\Tejesh Pawar\Documents\Spring23\Python_ZIA\assignment6_test\tests\test_assignment.py", line 13, in setUpClass assignment.normalize_database('non_normalized.db') File "c:\Users\Tejesh Pawar\Documents\Spring23\Python_ZIA\assignment6_test\assignment.py", line 468, in normalize_database exams = set((row[0], int(row[1])) for row in execute_sql_statement("""SELECT DISTINCT Exam, Year FROM Exams""", conn_non_normalized)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\Tejesh Pawar\Documents\Spring23\Python_ZIA\assignment6_test\assignment.py", line 420, in execute_sql_statement cur.execute(sql_statement) sqlite3.OperationalError: no such table: Exams
- Assistant
- User
- Assistant
Sorry about that, it looks like the
table_existsfunction was not defined correctly. Here is an updated version that should resolve the issue:# check if table exists def table_exists(table_name, conn): cur = conn.cursor() cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,)) return cur.fetchone() is not NoneThis should be defined outside of the
normalize_databasefunction, at the top level of theassignment.pyfile.You can replace the old
table_existsfunction with the updated one, and the error should be resolved.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.