SQLAlchemy&动态设置表列值

时间:2014-05-23 04:42:55

标签: python sqlalchemy

尝试这样做:

meta = MetaData(bind=engine)
meta.reflect(bind=engine, schema='schema')
Mapper(Table, meta.tables['schema.table'])

t = Table("schema.table", meta, autoload=True, autoload_with=engine)
map = {'Col1': 'full_local_col1_name', 'Col2': 'full_local_col2_name'}

for remote, local in map.items():
    t.set(local, values[remote])

这个想法是values是从另一个服务获得的Dictionary对象,我将列名映射到我的本地表。

我怎样才能做到这一点? SQLAlchemy的Table类没有set方法。

1 个答案:

答案 0 :(得分:1)

不确定您要做什么..您是否尝试插入,更新,更改或完全不同的内容?您可以执行此操作以进行插入。

meta = MetaData(bind=engine)
meta.reflect(bind=engine, schema='schema')
Mapper(Table, meta.tables['schema.table'])

t = Table("schema.table", meta, autoload=True, autoload_with=engine)
map = {'Col1': 'full_local_col1_name', 'Col2': 'full_local_col2_name'}

t.insert().values(**{local: values[remote] for remote, local in map.items()})
相关问题