Pandas to_sql fails on duplicate primary key

There is unfortunately no option to specify “INSERT IGNORE”. This is how I got around that limitation to insert rows into that database that were not duplicates (dataframe name is df)

for i in range(len(df)):
    try:
        df.iloc[i:i+1].to_sql(name="Table_Name",if_exists="append",con = Engine)
    except IntegrityError:
        pass #or any other action

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)