在现有的PostgreSQL表中,我希望使用字典查找(见下面的字典)更新多个现有列的值。类似于这篇不错的博客文章所描述的方式。然而,我无法想出如何使用Python字典来实现。以下是可怕的伪代码:
这是否可能,还是我必须遍历整个表格?
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 | 10 | 21
1 | 11 | 20
...
操作完成后,应该看起来像这样:
gid | furniture | plant
-----------------------------
0 | chair | peace lily
1 | table | english ivy
...
这是否可能,还是我必须遍历整个表格?