Untitled
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
# Check if the primary key exists in the target DataFrame if pk_value not in target_df[pk].values: # Generate an INSERT query dynamically insert_query = f"INSERT INTO {schema_ext}.{table_name} (" insert_columns = [] insert_values = [] for column_name, source_val in source_row.items(): if column_name not in ('created_by', 'created_on', 'updated_on', 'start_date', 'end_date'): 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 else: # Fetch the corresponding row from the target DataFrame based on the primary key target_row = target_df[target_df[pk] == pk_value].iloc[0] if not source_row.equals(target_row): # Generate an update query dynamically update_query = f"UPDATE {schema_ext}.{table_name} SET " column_updates = [] for column_name, source_val in source_row.items(): if column_name not in ('created_by', 'created_on', 'updated_on', 'start_date', 'end_date'): target_val = target_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 {eprm_table_col_pk} = '{pk_value}'" # print(update_query) try: print(update_query) # cursor_ext.execute(update_query) except Exception as e: continue