Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
1.5 kB
2
Indexable
Never
# Loop through rows and insert data
for _, row in df.iterrows():
    eprm_table_name = row['eprm_table_name'].lower()  # Convert table name to lowercase
    if eprm_table_name != 'pkg_prd_fed_ext_attrs':
        source_query = f"SELECT * FROM {schema_source}.{eprm_table_name} WHERE release_id='{releaseId}' AND op_id='{opId}' AND bu_id='{buId}'"
        try:
            source_df = pd_read_sql(source_query, con=conn_source)
            if not source_df.empty:
                # Exclude the 'created_by' column from the DataFrame before insertion
                columns_to_insert = [col for col in source_df.columns if col != 'created_by']
                source_df = source_df[columns_to_insert]

                source_df['created_by'] = 'REP_990_234'  # Add 'created_by' column with value 'REP_990_234'
                source_df.to_sql(eprm_table_name, con=target_engine, if_exists='append', index=False)
                write_sql(f"-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ STATUS|{replicationTarget}| TABLE| {eprm_table_name}\n")
                write_sql(f"-- #Query: Inserted {len(source_df)} record(s)\n")
                print(f"Inserting records into {eprm_table_name}")
                logging.info(f"Inserted {len(source_df)} record(s) into {eprm_table_name}")
        except mysql.connector.Error as err:
            print(f"Error occurred while executing the query: {source_query}: {err}")
            logging.info(f"Error occurred while executing the query: {source_query}: {err}")