当值为 '2"%' 时在 postgresql 中使用副本时出错

error using copy from in postgresql when value is '2"%'

我有一个 table,其中有 300,000 行不在我的控制之下,我需要导入。

我使用以下方法从 mysql 导出数据:

mysqldump -u root --password=secret --fields-enclosed-by='\"' -T/tmp apflora_beob

然后我尝试导入每个 table 使用例如:

\copy beob_evab FROM '/Users/alex/tmp/beob_evab.txt' (FORMAT 'csv', DELIMITER E'\t', NULL '\N', ESCAPE E'\"', ENCODING 'UTF8');

这适用于大多数 table,也适用于包含 450,000 行的行。但是在一个我得到这个错误:

ERROR:  value too long for type character varying(10)
CONTEXT:  COPY beob_evab, line 190310, column COUV_MOUSSES: "2\%  \N  \N  \N  \N  \N  \N  \N  \N  \N  \N  30  \N  15  \N  \N  \N  \N  \N  \N  \N  \N  \N  \N  \N  \N  0.01  \N  \N
Bachs, Dau..."

当我检查第 190'310 行中的字段 COUV_MOUSSES 时,它包含以下值:2"%.

我需要更改什么才能使其正常工作?

为了根据@klin 实现正确的输出 "2""%"(谢谢!),我必须将 --fields-escaped-by='"' 添加到 mysqldump 命令。这样就变成了:

mysqldump -u root --password=secret --fields-enclosed-by='"' --fields-escaped-by='"' -T/tmp apflora_beob

问题是:现在 Null 值导出为 "N.

嗯,没问题,我想了想,把复制命令改成了:

\copy beob_evab FROM '/Users/alex/tmp/beob_evab.txt' (FORMAT 'csv', DELIMITER E'\t', NULL '"N', ESCAPE "'", ENCODING 'UTF8');

这会在 Postgres 中产生以下错误:

ERROR:  CSV quote character must not appear in the NULL specification

并且在 MySQL 中似乎无法更改 Null 值的表示。

数值应记为

"2""%"

根据 RFC 4180 - Common Format and MIME Type for Comma-Separated Values (CSV) Files:

[5]. Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields.(...)

[7]. If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

   "aaa","b""bb","ccc"