sqlalchemy insert on duplicate update

from sqlalchemy.dialects.mysql import insert

insert_stmt = insert(my_table).values(
    id='some_existing_id',
    data='inserted value')

on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
    data=insert_stmt.inserted.data,
    status='U'
)

conn.execute(on_duplicate_key_stmt)


# another example
data = {"task_id": task_id,
"task": task,
"execution_time": execution_time,
"start_dt": start_dt,
"end_dt": end_dt,
"state": state}
insert_stmt = mysql.insert(TaskInstance).values(**data)
on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
{"start_dt": start_dt, "end_dt": end_dt})
session.execute(on_duplicate_key_stmt)
 

 

https://docs.sqlalchemy.org/en/13/dialects/mysql.html

posted on 2020-09-24 14:36  Go_Forward  阅读(953)  评论(0编辑  收藏  举报