Question: Python - I am trying to build this load_person function but keep getting stuck. I am not sure I fully follow. I have highlighted my
Python - I am trying to build this load_person function but keep getting stuck. I am not sure I fully follow. I have highlighted my concerns in yellow, these are the parts that I am not sure if I am doing correctly. What am I doing wrong here? Any advise is truly appreciated!!


import sqlite3 from sqlite3 import Error class PersonDB): context manager class provides read access to database def _init__(self, db_file=''): Store db_file parameter value self.db_file db_file def enter_(self): Initiate connection to database self.conn sqlite3.connect(self.db_file, check_same_thread=False) return self 8 A 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 def _exit__(self, exc_type, exc_value, exc_traceback): Close database connection self.conn.close() def load_person(self, id): sql = "SELECT * FROM people WHERE id=?" cursor = self.conn.cursor() cursor.execute(sql, (id, )) records = cursor.fetchall() result = (-1,'','') # id = -1, first_name "', last_name if records is not None and len(records) > 0: result = records[@] cursor.close() return result load_person() signature: def load_person(self, id): o This method will attempt to load a "people" record with the id of "id". o Create a sql string to select a specific record from the database: sql = "SELECT * FROM people WHERE id=?" 0 Next, open a cursor and use that cursor to execute the sql command: cursor = self.conn.cursor() cursor.execute(sql, (id)) records = cursor.fetchall() Note: the execute command accepts the sql command string and a tuple of parameters to substitute for the question marks in the command string. Notice the comma with nothing after it? That is to ensure (id,) results in a tuple, not a single evaluated value. Next create a variable called result and assign to it a tuple that represents an empty record if nothing is found in the database: result = (-1,'','') # id = -1, first_name = '', last_name o Finally, check to see if the "records" list contains any data and if it does get the first value out of the list. If you simply return records, you will get a list with one item in it instead of just the one item. if records is not None and len(records) > 0: result = records[0] cursor.close() return result import sqlite3 from sqlite3 import Error class PersonDB): context manager class provides read access to database def _init__(self, db_file=''): Store db_file parameter value self.db_file db_file def enter_(self): Initiate connection to database self.conn sqlite3.connect(self.db_file, check_same_thread=False) return self 8 A 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 def _exit__(self, exc_type, exc_value, exc_traceback): Close database connection self.conn.close() def load_person(self, id): sql = "SELECT * FROM people WHERE id=?" cursor = self.conn.cursor() cursor.execute(sql, (id, )) records = cursor.fetchall() result = (-1,'','') # id = -1, first_name "', last_name if records is not None and len(records) > 0: result = records[@] cursor.close() return result load_person() signature: def load_person(self, id): o This method will attempt to load a "people" record with the id of "id". o Create a sql string to select a specific record from the database: sql = "SELECT * FROM people WHERE id=?" 0 Next, open a cursor and use that cursor to execute the sql command: cursor = self.conn.cursor() cursor.execute(sql, (id)) records = cursor.fetchall() Note: the execute command accepts the sql command string and a tuple of parameters to substitute for the question marks in the command string. Notice the comma with nothing after it? That is to ensure (id,) results in a tuple, not a single evaluated value. Next create a variable called result and assign to it a tuple that represents an empty record if nothing is found in the database: result = (-1,'','') # id = -1, first_name = '', last_name o Finally, check to see if the "records" list contains any data and if it does get the first value out of the list. If you simply return records, you will get a list with one item in it instead of just the one item. if records is not None and len(records) > 0: result = records[0] cursor.close() return result
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
