Sqlalchemy Upsert 2024-10-01 Sqlalchemy►ORM ORM, Sqlalchemy 0 评论 字数统计: 83(字) 阅读时长: 1(分) 实现不存在则插入12345678def upsert(table, insert_data: dict, update_data: dict): """ 插入或者更新 """ insert_stmt = insert(table).values(**insert_data) on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(**update_data) with db.engine.connect() as connect: connect.execute(on_duplicate_key_stmt) 12345678def ignore_insert(table, data: dict): """ 忽略重复插入 """ insert_command = table.__table__.insert().prefix_with(' IGNORE').values(data) with db.engine.connect() as connect: connect.execute(insert_command) connect.commit()