【问题标题】:using copy from command to load data to postgres from csv使用 copy from 命令将数据从 csv 加载到 postgres
【发布时间】:2021-07-28 03:32:37
【问题描述】:
create table emp (emp_id integer,
    emp_name varchar(30),
    registered_course varchar(15),
    progress integer);

我在 csv 中有数据:

emp_id, emp_name, registered_course
1,"vignesh","java",10
2,"rakesh","",

即,在我的 csv 中,NULL 整数列数据表示为不带引号的空字符串,NULL varchar 数据表示为 ""

现在,当我尝试使用 copy from command 代替 postgres 的 int 列将 csv 上传到 Postgres 时,我可以看到 NULL 被插入,但是对于 varchar 我可以看到它是空白的/零长度字符串)。

到目前为止我的命令:

String copyQuery="COPY "+tableName+"("+columns+") FROM STDIN WITH DELIMITER ',' CSV HEADER";
    

long rowsInserted;
    
try {

    rowsInserted = new CopyManager((BaseConnection) conPost)

            .copyIn(

                copyQuery,new FileReader(filePath)

                );

}

在文档中:- https://www.postgresql.org/docs/current/sql-copy.html 我可以看到 NULL 指定了表示空值的字符串,默认情况下它是 CSV 格式的未加引号的空字符串。所以这就是为什么我可以看到 csv 中的空整数数据在加载后作为未引用的空字符串表示为 NULL

但是,现在我怎样才能将 csv 中的 NULL varchar 数据设置为 "" 在 postgres 中表示为 NULL ???截至目前,它表示为空字符串(空白/零长度)。

提前致谢

【问题讨论】:

  • 感谢@a_horse_with_no_name 提供当前文档并纠正我的问题。当前文件解决了我的问题:)

标签: postgresql csv


【解决方案1】:

使用 force_null,记录在您已链接的同一位置。

copy emp from stdin with (format csv, header, force_null (registered_course));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多