简体   繁体   English

使用psql复制csv时包含NULL值

[英]Include NULL values when copying csv using psql

I have a csv with various data formats that I am trying to copy in to postgres. 我有一个尝试将各种数据格式复制到Postgres的csv。 I am encountering this error every time because at line 97789 I have a few columns with empty cells. 我每次都会遇到此错误,因为在97789行,我有几列带有空单元格。

Rather than deleting this row (and other rows that may contain NULL values), I'd like to find a solution to including them in the database. 除了要删除此行(以及其他可能包含NULL值的行),我还想找到一种解决方案,将它们包括在数据库中。 Column ma effective date is supposed to take an integer value. ma effective date列应采用整数值。 But when I try to use the following code - it doesn't work, probably because I am trying to input NULL as a string: 但是,当我尝试使用以下代码时,它不起作用,可能是因为我试图将NULL作为字符串输入:

oakland=# \copy test_import FROM '/Users/Downloads/IE670_test.csv' DELIMITER ','  NULL AS 'NULL' csv
ERROR:  invalid input syntax for integer: "        "
CONTEXT:  COPY test_import, line 97789, column ma effective date (ccyymmdd): "        "

however, switching to NULL as NULL also doesn't work and produces a syntax error in psql . 但是,将NULL as NULL切换NULL as NULL也不起作用,并在psql产生语法错误。 Is there an effective solution to including NULL values regardless of the data type in the column? 是否有有效的解决方案,无论列中的数据类型如何,都包含NULL值?

Here is the offending line in raw format: 这是原始格式的违规行:

048A706200600,48A-7062-6     ,17,3,5815,PRESLEY WAY                                       ,          ,OAKLAND                       ,94618,    ," $102,150.00 "," $38,259.00 ", $-   , $-   , $-   , $-   , $-   ," $7,000.00 ", $-   ," $133,409.00 ",2015,139807,20150527,20150901, ,                    ,                                                  ,                                                            ,          ,                              ,     ,    ,  , ,        , ,1100,Single family residential homes used as such

If the null-valued cells are truly empty, omit the NULL AS 'NULL' clause. 如果空值单元格确实为空,则省略NULL AS 'NULL'子句。 Otherwise (as appears to be the case), replace the 'NULL' string with a string containing whatever is in those cells. 否则(似乎是这种情况),将'NULL' 字符串替换为包含这些单元格中任何内容的字符串。 You may need to ensure that the null values in the csv are consistent. 您可能需要确保csv中的空值一致。

test.csv (one-space null): test.csv(一个空格为null):

1, ,3

SQL 的SQL

create table csv (one int, two int, three int);

One space as null: 一个空格为null:

\copy csv from '~/test.csv' delimiter ',' null as ' ' csv;
COPY 1

Two spaces as null: 两个空格为null:

\copy csv from '~/test.csv' delimiter ',' null as '  ' csv;
ERROR:  invalid input syntax for integer: " "
CONTEXT:  COPY csv, line 1, column two: " "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM