小编dtl*_*her的帖子

如何将字典项插入到 PostgreSQL 表中

因此,我使用 Psycopg2 将 Postgresql 数据库连接到 Python,并且提取了两个特定列进行更新。我将第一列用作 Python 字典中的键,并在第二列上运行一些函数并将结果用作字典中的值。现在我想做的是将这些值作为新列添加回 Postgresql 表中,但我希望它们与字典中配对的正确键配对。本质上,我想获取字典值并将它们作为新列插入,并选择它们属于 Postgresql 表中的哪个“键”(但是,我不想手动分配它们,因为,好吧,希望有更好的方法)。

PostgreSQL 表

     |col1   |col2  |col3   | ... | coln  
row1 | a1    | b1   | c1    | ... | n1  
row2 | a2    | b2   | c2    | ... | n2  
...  | ...   | ...  | ...   | ... | n...  
rowm | am    | bm   | cm    | ... | nm
Run Code Online (Sandbox Code Playgroud)

这是我用Python制作的字典,其中f()有一系列在变量上运行的函数:

{ 
    a1 : f(c1),  
    a2 : f(c2),   
    ... : ... 
}
Run Code Online (Sandbox Code Playgroud)

现在我的目标是将值列添加回我的表中,以便它与原始键相对应。理想情况下,看起来像这样: …

python postgresql dictionary psycopg2 key-value

5
推荐指数
1
解决办法
4840
查看次数

标签 统计

dictionary ×1

key-value ×1

postgresql ×1

psycopg2 ×1

python ×1