'Connect to the database, and create tables if necessary.'

@catch_corrupt_db
def init_db(self):

    if (not self.enabled):
        self.db = DummyDB()
        return
    kwargs = dict(detect_types=(sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES))
    kwargs.update(self.connection_options)
    self.db = sqlite3.connect(self.hist_file, **kwargs)
    self.db.execute('CREATE    TABLE    IF    NOT    EXISTS    sessions    (session    integer\n                                                                                                primary    key    autoincrement,    start    timestamp,\n                                                                                                end    timestamp,    num_cmds    integer,    remark    text)')
    self.db.execute('CREATE    TABLE    IF    NOT    EXISTS    history\n                                                                (session    integer,    line    integer,    source    text,    source_raw    text,\n                                                                PRIMARY    KEY    (session,    line))')
    self.db.execute('CREATE    TABLE    IF    NOT    EXISTS    output_history\n                                                                                                (session    integer,    line    integer,    output    text,\n                                                                                                PRIMARY    KEY    (session,    line))')
    self.db.commit()
    self._corrupt_db_counter = 0
