def dict_factory(cursor, row):
    """Convert database row objects to a dictionary keyed on column name.

    This is useful for building dictionaries which are then used to
    render a template.  Note that this would be inefficient for large
    queries.
    """
    return {col[0]: row[idx] for idx, col in enumerate(cursor.description)}