def cursor_to_data_header(cursor):
    """Fetches all rows from query ("cursor") and returns a pair (data, header)

    Returns: (data, header), where
        - data is a [num_rows]x[num_cols] sequence of sequences;
        - header is a [num_cols] list containing the field names
    """
    n = 0
    data, header = [], {}
    for row in cursor:
        if n == 0:
            header = row.keys()
        data.append(row.values())
    return data, list(header)