0

I am storing pandas df into mysql using sqlalchemy as below,

db_connection = sql_engine.connect()
frame = pd_df.to_sql(table_name, db_connection, if_exists='append', index=False)

where primary keys is defined and column exists in df itself. I want to update all the columns of the row if there are multiple records with same primary key exists in df.

I don't want to ignore the rows but update all the column values for that id.

How to achieve this ?

j '
  • 191
  • 1
  • 2
  • 12
  • Does this answer your question? [Pandas to\_sql() to update unique values in DB?](https://stackoverflow.com/questions/52188446/pandas-to-sql-to-update-unique-values-in-db) – Danila Ganchar Jan 11 '20 at 15:39
  • No as i don't want to replace/ignore the row . I need to update values of columns keeping the same primary key id. – j ' Jan 13 '20 at 10:10
  • 1
    Use `to_sql` to populate a temporary table and then execute a [INSERT ... SELECT ... ON DUPLICATE KEY UPDATE](https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html) statement. – Gord Thompson Jan 13 '20 at 12:10

0 Answers0