在現有的PostgreSQL表中,我想用UPDATE
幾個現有的字段查找字段值(請參閱下面的代碼段)。有點像nice blog post中描述的那樣。但是,我不知道如何用Python字典來做到這一點。這裏談到的可怕的僞代碼:批量更新Python中的PostgreSQL字典
d = {10:'chair', 11:'table', 12:'lamp',
20:'english ivy', 21:'peace lily', 22:'spider plant'}
curs.execute("""
UPDATE my_table t
SET furniture = %(t.furniture)s,
SET plant = %(t.plant)s""",
d)
的原表看起來有點像這樣:
gid | furniture | plant
-----------------------------
0 | chair | peace lily
1 | table | english ivy
...
:
gid | furniture | plant
-----------------------
0 | 10 | 21
1 | 11 | 20
...
操作就應該是這樣的後
這是可能的還是我將不得不通過表循環?
http://stackoverflow.com/questions/7019831/bulk-batch-update-upsert-in-postgresql – Ashalynd
你知道SQLAlchemy嗎? – Ashalynd
@Ashalynd謝謝!我很確定鏈接的問題如何解決我的問題 - 對不起。我玩了SQLAlchemy的ORM。但顯然我不太清楚。它有一個工具嗎? – n1000