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.
I'd like to do the equivalent of insert ignore
when trying to append
to the existing table, so I would avoid a duplicate entry error.
Is this possible with pandas, or do I need to write an explicit query?
When you use SQL Server you'll get a SQL error when you enter a duplicate value into a table that has a primary key constraint. You can fix it by altering your table: CREATE TABLE [dbo].
Pandas DataFrame.to_sql method has limitation of not being able to "insert or replace" records, see e.g: pandas-dev/pandas#14553 Using pandas.io.sql primitives, however, it's not too hard to implement such a functionality (for the SQLite case only).
Yes, please. 'append_skipdupes' should be added and not only for the Primary Key column. If there are duplicates among other Unique columns also it should skip appending those new duplicate rows. Sorry, something went wrong. Sorry, something went wrong.
If there are duplicates among other Unique columns also it should skip appending those new duplicate rows. Sorry, something went wrong. Sorry, something went wrong. Sorry, something went wrong.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With