Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python - Help with creating a context manager class that will provide read access to the database created in another assignment. I am stuck on

Python - Help with creating a context manager class that will provide read access to the database created in another assignment. I am stuck on the yellow highlights in the assignment. You can see my own attempts in the screen shots. What am I going wrong here? please help.

image text in transcribedimage text in transcribed

The next part of the lab is to create a context manager class called PersonDB that will provide read access to the database created in part II: class PersonDBO: _init__() signature: def __init__(self, db_file="): For this method, all that needs to be done is to store the db_file parameter value into a self.db_file variable. o You will use it in the_enter__) method. o enter_signature: def enter_(self): _enter_represents the method that is called when a class object of type PersonDB is used within a "with" context. o This method will initiate the connection to a SQLite database indicated by self.db_file. o Recall from Part II how to call the connect() method of sqlite3. Store the resulting connection object in a variable called self.conn. Return self O def _exit_ signature: exit__(self, exc_type, exc_value, exc_traceback): exit_represents the method that is called after the last line of a with block is executed. This method will close the database connection by calling self.copo.close(). class PersonDB(): def 7 8 9 10 11 12 13 14 15 16 _init__(self, db_file=''): self.db_file = db_file def enter_(self): # enter represents the method that is called when a class object of type PersonDB is #used within a "with" context. #This method will initiate the connection to a SQLite database indicated by self.db_file. 17 self._enter 18 19 20 21 22 return self.conn 23 #Recall from Part II how to call the connect() method of sqlite3. Store the resulting #connection object in a variable called self.conn. #Return self 24 25 26 27 28 29 30 31 32 33 34 _exit_0 signature: def exit_(self, exc_type, exc_value, exc_traceback): if self.db_file == None: return 35 36 37 self.conn.close() exit represents the method that is called after the last line of a with block is executed. #This method will close the database connection by calling self.conn.close(). # 38 39 40 41 42

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

=+Construct a data- and research-driven SWOT analysis

Answered: 1 week ago

Question

What is electric dipole explain with example

Answered: 1 week ago

Question

What is polarization? Describe it with examples.

Answered: 1 week ago