SQLAlchemy INSERT IGNORE

后端 未结 2 776
走了就别回头了
走了就别回头了 2020-12-14 07:37

How can I insert multiple data records into table ignoring duplicates. I am using SQLAlchemy.

Thank you!

2条回答
  •  轮回少年
    2020-12-14 08:01

    prefix_with("TEXT") adds arbitrary text between INSERT and the rest of the SQL. execute() accepts a list of dictionaries with the records you would like to insert or a single dictionary if you only want to insert a single record.

    The SQLite syntax for the behavior you're looking for:

    inserter = table_object.insert().prefix_with("OR REPLACE")
    inserter.execute([{'column1':'value1'}, {'column1':'value2'}])
    

提交回复
热议问题