gpt4 book ai didi

postgresql - 使用默认值从 csv 复制

转载 作者:行者123 更新时间:2023-11-29 13:23:07 24 4
gpt4 key购买 nike

我有 CSV 文件要导入到包含许多列的 PostgreSQL 表中。我首先创建一个临时表,然后在其中复制 CSV 文件...

DROP TABLE IF EXISTS mylist;
CREATE TEMP TABLE mylist (a text, b text, c text, d text, e text, f text,
g text, h text, i text, j text, k text, l text,
m text, n text, o text, p text, q text, r text,
s text, t text, u text, v text, w text,
CONSTRAINT popisp_pkey PRIMARY KEY(A));

COPY mylist FROM 'C:\temp\popisp1.csv' DELIMITER AS ';' CSV;

这会将数据复制到表中,但会出现其他问题。在没有任何数据的字段中,似乎复制了 Null,但我需要一个空字符串 ('')。

这里有什么方法可以自动获取表格单元格中没有数据的空字符串?
我尝试将 DEFAULT '' 添加到每个列声明但没有成功。

最佳答案

添加 NULL 参数,其中包含肯定不会出现在文件中的字符串:

COPY mylist FROM 'C:\temp\popisp1.csv' DELIMITER AS ';' NULL '<<>>' CSV;

根据 the documentation :

NULL

Specifies the string that represents a null value. The default is \N (backslash-N) in text format, and an unquoted empty string in CSV format. You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings. This option is not allowed when using binary format.

或者使用coalesce() :

update mylist set
a = coalesce(a, ''),
b = coalesce(b, ''),
-- ...
w = coalesce(w, '');

关于postgresql - 使用默认值从 csv 复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38191830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com