Postgresql如何在conflic上使用CSV到CSV进行复制?
问题内容:
我想要做
" on conflict (time) do update set name , description "
但是我不知道何时将stdin与csv一起使用,我不知道什么名字等于什么?和描述等于…
table_a:
xxx.csv:
with open('xxx/xxx.csv', 'r', encoding='utf8') as f:
sql = """
COPY table_a FROM STDIN With CSV on conflict (time)
do update set name=??, description=??;
"""
cur.copy_expert(sql, f)
conn.commit()
问题答案:
感谢每个主人的解决方案。
这是我的解决方案。
sql = """
CREATE TABLE temp_h (
time ,
name,
description
);
COPY temp_h FROM STDIN With CSV;
INSERT INTO table_a(time, name, description)
SELECT *
FROM temp_h ON conflict (time)
DO update set name=EXCLUDED.name, description=EXCLUDED.description;
DROP TABLE temp_h;
"""