Untitled
unknown
plain_text
a year ago
2.3 kB
4
Indexable
# ... (previous code) # Merge source and target DataFrames on the primary key column merged_df = source_df.merge(target_df, on=pk, how='left') # Rows that are in source but not in target need to be inserted insert_df = merged_df[merged_df[pk].isnull()] # Rows where values are different between source and target need to be updated update_df = merged_df[(~merged_df[pk].isnull()) & ( merged_df.drop(columns=pk) != merged_df.drop(columns=pk))] # Check if insert_df is not empty before performing insertions if not insert_df.empty: for index, row in insert_df.iterrows(): pk_value = row[pk] table_name = row['your_table_name'] # Replace 'your_table_name' with the actual table name insert_query = f"INSERT INTO {schema_ext}.{table_name} (" insert_columns = [] insert_values = [] for column_name, source_val in row.items(): if column_name != pk: insert_columns.append(column_name) insert_values.append(f"'{source_val}'") insert_query += ", ".join(insert_columns) insert_query += ") VALUES (" insert_query += ", ".join(insert_values) insert_query += ");" # Execute the INSERT query try: print(insert_query) # cursor_ext.execute(insert_query) except Exception as e: continue # Check if update_df is not empty before performing updates if not update_df.empty: for index, row in update_df.iterrows(): pk_value = row[pk] table_name = row['your_table_name'] # Replace 'your_table_name' with the actual table name update_query = f"UPDATE {schema_ext}.{table_name} SET " column_updates = [] for column_name, source_val in row.items(): if column_name != pk: target_val = row[column_name] if source_val != target_val: column_updates.append(f"{column_name} = '{source_val}'") update_query += ", ".join(column_updates) update_query += f" WHERE {pk} = '{pk_value}';" # Execute the UPDATE query try: print(update_query) # cursor_ext.execute(update_query) except Exception as e: continue # ... (the rest of your code)
Editor is loading...