You cannot use Pandas! I will deduct points after manual check if you use Pandas. You…
- User
You cannot use Pandas! I will deduct points after manual check if you use Pandas. You CANNOT use the 'csv' module to read the file
Hint: Ensure to strip all strings so there is no space in them
DO NOT use StudentID from the non_normalized table. Let the normalized table automatically handle StudentID.
def create_connection(db_file, delete_db=False): import os if delete_db and os.path.exists(db_file): os.remove(db_file)
conn = None try: conn = sqlite3.connect(db_file) conn.execute("PRAGMA foreign_keys = 1") except Error as e: print(e) return conndef create_table(conn, create_table_sql): try: c = conn.cursor() c.execute(create_table_sql) except Error as e: print(e)
def execute_sql_statement(sql_statement, conn): cur = conn.cursor() cur.execute(sql_statement)
rows = cur.fetchall() return rowsconn_non_normalized = create_connection('non_normalized.db')
sql_statement = "select * from Students;"
df = pd.read_sql_query(sql_statement, conn_non_normalized)
display(df)
def normalize_database(non_normalized_db_filename):
Normalize 'non_normalized.db'
Call the normalized database 'normalized.db'
Function Output: No outputs
Requirements:
Create four tables
Degrees table has one column:
[Degree] column is the primary key
Exams table has two columns:
[Exam] column is the primary key column
[Year] column stores the exam year
Students table has four columns:
[StudentID] primary key column
[First_Name] stores first name
[Last_Name] stores last name
[Degree] foreign key to Degrees table
StudentExamScores table has four columns:
[PK] primary key column,
[StudentID] foreign key to Students table,
[Exam] foreign key to Exams table ,
[Score] exam score
### BEGIN SOLUTION pass ### END SOLUTIONnormalize_database('non_normalized.db')
conn_normalized = create_connection('normalized.db')
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_statementcan you complete normalize_database() and ex30() functions?
- Assistant
Yes, I can complete normalize_database() and ex30() functions.
- User
complete those functions and send only those two functions
- Assistant
- User
insert_into_degrees = "INSERT INTO Degrees (Degree) VALUES ("{}")".format(degree[0]) There is something error in this line
- Assistant
You are correct, there is an error in that line. The quotes around
{}should be single quotes ('{}'). Here's the corrected line:insert_into_degrees = "INSERT INTO Degrees (Degree) VALUES ('{}')".format(degree[0]) - User
AttributeError: '_ErrorHolder' object has no attribute '_testMethodName'
this error is coming when i'm running the above code
- Assistant
This error usually occurs when you are trying to use the
unittestmodule's assertions outside of a test method.It seems like some of the code you're running is a test case written using the
unittestframework. If you're not running the code as a test, you can remove or comment out the test code and the error should go away.Otherwise, you will need to make sure that all of the
unittestassertions are being used inside of a test method. Here's an example of a basic test class that uses someunittestassertions:import unittest class TestDatabase(unittest.TestCase): def setUp(self): # code to set up test data or test environment pass def tearDown(self): # code to clean up after test pass def test_database(self): # test code that uses unittest assertions self.assertEqual(1 + 1, 2) self.assertTrue(True) if __name__ == '__main__': unittest.main()In this example, the
setUpandtearDownmethods are used to set up and clean up the test environment, and the method contains the actual test code that uses the assertions. The block runs the tests when the script is run directly. - User
how to fix it in my case
- Assistant
Can you please provide more information regarding which part of the code is giving the error?
Based on the error message, it seems like the error is related to the
unittestmodule. If you are not running the code as a test, you can try removing anyunittestspecific code to see if that fixes the error.If you are running the code as a test, make sure that you are using the
unittestassertions inside of a test method. Here's an example of a basic test class that uses someunittestassertions:import unittest class TestNormalizeDatabase(unittest.TestCase): def setUp(self): # code to set up test data or test environment pass def tearDown(self): # code to clean up after test pass def test_normalize_database(self): # test code that uses unittest assertions self.assertEqual(1 + 1, 2) self.assertTrue(True) if __name__ == '__main__': unittest.main() - User
Traceback (most recent call last): File "c:\Users\Tejesh Pawar\Documents\Spring23\Python_ZIA\assignment6\run_tests.py", line 13, in JSONTestRunner(visibility='visible', stream=f).run(suite)
from this it has started to throw error
- Assistant
It seems like you are using the
JSONTestRunnerclass from thexmlrunnermodule to run your tests and generate JSON output.Without more information about the error message, it is difficult to determine the exact cause of the issue. However, here are some common causes and solutions for similar errors:
-
AttributeError: '_ErrorHolder' object has no attribute '_testMethodName'This error typically occurs when you are trying to use the
unittestmodule's assertions outside of a test method. Check whether your code is usingunittestassertions outside of the test methods. To solve this error, remove anyunittestspecific code outside of the test methods. -
AttributeError: '_Outcome' object has no attribute 'exc_info'This error may be caused by a version incompatibility between
xmlrunnerandunittest. Try installing a different version ofxmlrunneror upgrading/downgrading your version of .
-
- User
- Assistant
Based on the error message, it seems like the
normalize_databasefunction is throwing an exception:sqlite3.OperationalError: no such table: StudentExamScoresThis error suggests that the
StudentExamScorestable does not exist in the non-normalized database (non_normalized.db).Double-check that the
StudentsandStudentExamScorestables have been created in the non-normalized database and that they have data in them. If these tables don't exist or are empty, it could cause the normalization process to throw an error.Also, make sure that the database file paths in your
create_connectionfunction are correct and that the non-normalized database file is located in the specified path.Once you have addressed this issue, try running your test code again to see if the error is resolved.
- User
can you fix those functions and send the updated one which doesn't give any error and runs smooth
- Assistant
Yes, I can try to fix the functions. Here are the updated functions:
import sqlite3 def create_connection(db_file, delete_db=False): import os if delete_db and os.path.exists(db_file): os.remove(db_file) conn = None try: conn = sqlite3.connect(db_file) conn.execute("PRAGMA foreign_keys = 1") except sqlite3.Error as e: print(e) return conn def create_table(conn, create_table_sql): try: c =