Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.9 kB
2
Indexable
Never
# ... (previous code)

# 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 + '_source']
        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 + '_source':
                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 + '_source']
        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 + '_source':
                target_val = row[column_name + '_target']
                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)