Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
# Initialize counters
insert_success_count = 0
insert_failed_count = 0
update_success_count = 0
update_failed_count = 0

# ... (previous code)

for index, source_row in source_df.iterrows():
    pk_value = source_row[pk]

    # Check if the primary key exists in the target DataFrame
    if pk_value not in target_df[pk].values:

        # ... (code for constructing the INSERT query)

        # Execute the INSERT query
        if insert_columns:  # Check if there are columns to insert
            try:
                cursor_ext.execute(insert_query)
                # connection_ext.commit()
                insert_success_count += 1  # Increment insert success count
            except Exception as e:
                logging.error(
                    "Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))
                insert_failed_count += 1  # Increment insert failed count
    else:
        # ... (code for updating columns)

        # Execute the UPDATE query
        if columns_to_update:  # Check if there are columns to update
            try:
                cursor_ext.execute(update_query)
                # connection_ext.commit()
                update_success_count += 1  # Increment update success count
            except Exception as e:
                logging.error(
                    "Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))
                update_failed_count += 1  # Increment update failed count

# Print the counts for inserts and updates
print(f"Successful inserts: {insert_success_count}")
print(f"Failed inserts: {insert_failed_count}")
print(f"Successful updates: {update_success_count}")
print(f"Failed updates: {update_failed_count}")

# ... (remaining code)