'Add the format specified by ext. If it already exists it is replaced.'

def add_format(self, index, ext, stream, index_is_id=False):

    id = (index if index_is_id else self.id(index))
    stream.seek(0, 2)
    usize = stream.tell()
    stream.seek(0)
    data = sqlite.Binary(compress(stream.read()))
    exts = self.formats(index, index_is_id=index_is_id)
    if (not exts):
        exts = []
    if (not ext):
        ext = ''
    ext = ext.lower()
    if (ext in exts):
        self.conn.execute('UPDATE    data    SET    data=?    WHERE    format=?    AND    book=?', (data, ext, id))
        self.conn.execute('UPDATE    data    SET    uncompressed_size=?    WHERE    format=?    AND    book=?', (usize, ext, id))
    else:
        self.conn.execute('INSERT    INTO    data(book,    format,    uncompressed_size,    data)    VALUES    (?,    ?,    ?,    ?)', (id, ext, usize, data))
    self.conn.commit()
