在现有的 PostgreSQL 表中,我想UPDATE
几个现有列,其值来自字典查找(请参见下面的字典)。有点像这里描述的不错的博文 http://tapoueh.org/blog/2013/03/15-batch-update。但是,我不知道如何使用 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)
The original表看起来有点像这样:
gid | furniture | plant
-----------------------
0 | 10 | 21
1 | 11 | 20
...
After操作应该是这样的:
gid | furniture | plant
-----------------------------
0 | chair | peace lily
1 | table | english ivy
...
这是可能的还是我必须循环遍历表格?
尝试这个:
rows = (
{'gid': 10, 'furniture': 10, 'plant': 10},
{'gid': 20, 'furniture': 20, 'plant': 20}
)
cur.executemany(
'''
UPDATE myTable
SET
furniture = %(furniture)s,
plant = %(plant)s
WHERE
gid = %(gid)s
''',
rows
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)