python - 使用多个条件使用 SQLAlchemy/SQLite 更新连接
问题描述
我已经使用以下代码在 SQLite 数据库上成功设置了 ORM 以反映数据库:
Base = automap_base()
conn_string = 'sqlite:///' + wd.replace('\\','\\\\') + sqlite_filename
engine = create_engine(conn_string, echo=True)
@event.listens_for(Table, "column_reflect")
def reflect_col(inspector, table, column_info):
column_info['key'] = column_info['name'].replace(' ', '_')
Base.prepare(engine, reflect=True)
PpoAward = Base.classes.ppo_award
Accident = Base.classes.accident
session = Session(engine)
我成功地执行了这样的基本查询:
# delete existing accident records for this quarter
session.query(Accident).\
filter(and_(Accident.accounting_year==acct_year,
Accident.accounting_qtr == acct_qtr)).delete()
session.commit()
我正在尝试对 ppo_award 表进行更新连接,但是对此相对较新,我完全迷失在 SQLAlchemy 文档中。我认为 stackoverflow 上还没有一个直接的答案。
我希望能够在 Python 中以一种优雅的方式复制这个 SQL:
UPDATE ppo_award
SET p.ppo_award_accident_id = a.accident_id
FROM ppo_award p
INNER JOIN accident a ON p.field_1=a.field_1 AND p.field_2=a.field_2 AND p.field_3=a.field_3
字段 1、2 和 3 在事故表中具有唯一约束。
任何帮助将不胜感激。
解决方案
推荐阅读
- r - 我将 stat_regline_equation 与 ggscatter 一起使用。有没有办法指定系数的有效数字?
- angular - 有没有办法以角度对服务进行分组?
- r - 确定对象中的序列是否包含 1 和 4,以及所有其他数字是否介于 1 和 4 之间的函数
- typescript - 承诺 {
} 在 Deno(打字稿) - c++ - 查找类实例是否包含在 std::vector (C++) 中的优雅方法
- javascript - 如何设置相对于其他元素的图像大小
- powershell - 在 Powershell 中使用 Invoke-RestMethod 进行复杂查询
- react-native - 是否有使用 react-navigation 的代码手动解析我的深层链接的 API?
- typescript - 尝试在 Typescript 中设置 protobuf Map 条目
- python - 如何更快地运行 selenium webdriver 代码?