'The database isn\'t opened until first use.  This function ensures it is now open.'

def _ensure_db(self):

    if (not self._db):
        if (not self.dbfilename):
            self.dbfilename = ':memory:'
        self._db = apsw.Connection(self.dbfilename, flags=((apsw.SQLITE_OPEN_URI | apsw.SQLITE_OPEN_READWRITE) | apsw.SQLITE_OPEN_CREATE))
    return self._db
