Let's first have a look at the TaskDB
class. It consists of a constructor __init__()
that takes the filename where the database will reside as a parameter. It calls a private method to initialize this database, and like the LogonDB
class, creates some storage to hold connection objects for each thread (highlighted). It also defines a connect()
method that should be called once for each thread and stores a thread-specific connection object. It also sets the row_factory
attribute of the connection to sqlite3.Row
. This causes the tuples returned by, for example, fetchall()
to have their fields named after the columns they represent. This makes sense as t['user_id']
is a lot more self documenting than t[1]
, for example.
Chapter4/tasklistdb.py
class TaskDB:
def __init__(self,db):
self.data = threading.local()
self.db = db
self._initdb()
def connect(self):
'''call once for every thread'''
self.data.conn = sqlite3.connect...