首页 > 解决方案 > 使用 sqlalchemy 从 PostgreSQL 数据库中的列更新单个值

问题描述

我的 postgresql 数据库上有一个“批处理”表,我想更改“id”列中的单个值,将 70 替换为 15:

from sqlalchemy import create_engine

# Connection with DB
engine = create_engine('postgresql://xxx')

engine.connect()

# Update the value
...

标签: pythonpostgresql

解决方案


# Connection with DB
engine = create_engine('postgresql://xxx')
engine.connect()

# Update the value

sql = f"""
    UPDATE batch
    SET id=15 
    WHERE id=70
"""

with engine.begin() as conn:     # TRANSACTION
    conn.execute(sql)

推荐阅读