How to delete rows from a table using an SQLAlchemy query without ORM?

前端 未结 2 1078
挽巷
挽巷 2020-12-15 15:18

I\'m writing a quick and dirty maintenace script to delete some rows and would like to avoid having to bring my ORM classes/mappings over from the main project. I have a qu

2条回答
  •  余生分开走
    2020-12-15 16:19

    Looking through some code where I did something similar, I believe this will do what you want.

    d = addresses_table.delete().where(addresses_table.c.retired == 1)
    d.execute()
    

    Calling delete() on a table object gives you a sql.expression (if memory serves), that you then execute. I've assumed above that the table is bound to a connection, which means you can just call execute() on it. If not, you can pass the d to execute(d) on a connection.

    See docs here.

提交回复
热议问题