我正在尝试使用sqlalchemy构建一个delete查询。delete子句的where部分应该动态构造以满足多个条件。例如:
DELETE FROM table
WHERE table.col1 = x1
AND table.col2 = x2
AND ...
下面是我的代码的简化部分。
def upsert(key_col):
...
# Build the WHERE clause of your DELETE statement from rows in the dataframe.
cond = df.apply(lambda row: sa.and_(detail_table.c[key_col] == row[key_col]), axis=1)
cond = sa.or_(*cond)
# Define and execute the DELETE
delete = detail_table.delete().where(cond)
with engine.connect() as conn:
conn.execute(delete)
...
对于从具有单个主键列的表中删除行来说,这种方法运行得很好。我想通过考试 key_col
作为具有复合主键的表的列表。
我可以做一些像
cond = df.apply(lambda row: sa.and_(detail_table.c[key_col[0]] == row[key_col[0]],
detail_table.c[key_col[1]] == row[key_col[1]],
detail_table.c[key_col[2]] == row[key_col[2]]),
axis=1)
但我希望能够在运行时根据 key_col
. 我猜有更好的方法可以使用sqlalchemy执行这样的删除查询。
1条答案
按热度按时间s8vozzvw1#
sqlalchemy将接受多个
.where
构造并将它们结合在一起,例如。,