Pandas to_sql fails on duplicate primary key

后端 未结 6 1044
执笔经年
执笔经年 2020-12-28 18:13

I\'d like to append to an existing table, using pandas df.to_sql() function.

I set if_exists=\'append\', but my table has primary keys.

6条回答
  •  孤城傲影
    2020-12-28 18:24

    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
    

提交回复
热议问题