繁体   English   中英

使用SQLAlchemy替换MySQL字符串

[英]MySQL string replace using SQLAlchemy

SQLAlchemy是否有一种方法可以执行MySQL字符串替换。 我想找到一种在SQLAlchemy核心中执行以下操作的方法。

UPDATE your_table
SET your_field = REPLACE(your_field, 'from_str', 'to_str')
WHERE your_field LIKE '%from_str%'

是的,在Column Elements and Expressions上有更多的了解,但是关键是要使用func.MY_FUNCTION

func.REPLACE(YourTable.your_field, 'from_str', 'to_str')

如果您从查询执行更新,则可能看起来像这样:

updated_rows = (
    session.query(YourTable)
    .filter(YourTable.your_field.like('%est%'))
    .update({YourTable.your_field: func.replace(YourTable.your_field, 'from_str', 'to_str')},
            synchronize_session=False)
    )
print("Updated {} rows".format(updated_rows))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM