Walk the six generic database-access steps in order against a mock connection, printing each step as it happens and the rows it returns.
Walk the six generic database-access steps in order against a mock connection, printing each step as it happens and the rows it returns.
Answer
# A mock 'driver' + 'connection' so we can show the SIX universal steps in order. class Cursor: def __init__(self, rows): self._rows = rows def execute(self, query): print(f"step 3: execute -> {query}") return self def fetchall(self): return self._rows class Connection: def __init__(self): self.closed = False def cursor(self): return Cursor([(1001, 'Okafor'), (1002, 'Singh')]) def close(self): self.closed = True class Driver: name = 'mock-driver' def connect(self): return Connection() def access_database(): driver = Driver() # step 1: register the driver print(f"step 1: registered {driver.name}") conn = driver.connect() # step 2: open a connection print("step 2: connection opened") cur = conn.cursor() for row in cur.execute('SELECT id, name FROM students').fetchall(): # steps 3-4 print(f"step 4: row {row[0]} {row[1]}") # step 5: repeat 3-4 as needed (one pass here) conn.close() # step 6: close the connection print(f"step 6: connection closed = {conn.closed}") access_database()