def update_table(connector, season, sum_type, table, id_map): data = fetch_summary(season, sum_type, PLAYOFFS_ID) if not data: raise ValueError('Could not retrieve JSON') query_list = [] query = 'UPDATE {} SET {} WHERE id={}' for item in data['data']: vals = [] for key, value in item.iteritems(): if key == id_map: continue # This attribute lists all teams the player has played for. # We only care about the last (most recent) one. if key == 'playerTeamsPlayedFor': value = value.split(', ')[-1] if isinstance(value, (str, unicode)): value = '"' + value.encode('utf-8') + '"' vals.append('{}={}'.format(key, value)) query_list.append(query.format(table, ','.join(vals), item[id_map])) sql_transaction(connector, query_list)
def populate(connector, season, sum_type, table, col_map): data = fetch_summary(season, sum_type, REGULAR_SEASON_ID) if not data: raise ValueError('Could not retrieve JSON') columns = [] keys = [] for column, key in col_map.iteritems(): columns.append(column) keys.append(key) query = 'INSERT INTO {} ({}) VALUES '.format(table, ','.join(columns)) rows = [] for item in data['data']: row_args = [] for key in keys: val = item[key] # This attribute lists all teams the player has played for. # We only care about the last (most recent) one. if key == 'playerTeamsPlayedFor': val = val.split(', ')[-1] if isinstance(val, (str, unicode)): val = '"' + val.encode('utf-8') + '"' else: val = str(val) row_args.append(val) rows.append('(' + ','.join(row_args) + ')') query += ','.join(rows) sql_transaction(connector, query)