复制期间的 PostgreSQL 转义字符
PostgreSQL escape character during copy
我正在尝试将 CSV 文件导入 PostgreSQL,但遇到特殊字符问题。
我正在使用以下命令
./psql -d data -U postgres -c "copy users from 'users.csv' delimiter E'\t' quote '~' csv"
它工作正常,直到它遇到一个带有“~”的字段,我将其用作引号值以不破坏现有的引号和引号等。
如何在 csv 文件 'Person~name' 中转义此字符,以便将其导入为 'Person~name'
CSV 规则列在 https://www.ietf.org/rfc/rfc4180.txt
中
要在字符串中嵌入引号字符:
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"
在您的情况下,将双引号替换为波浪号,因为您已经选择了该分隔符。
示例:
test=> create table copytest(t text);
CREATE TABLE
test=> \copy copytest from stdin delimiter E'\t' quote '~' csv
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> ~foo~~bar~
>> \.
test=> select * from copytest;
t
---------
foo~bar
我正在尝试将 CSV 文件导入 PostgreSQL,但遇到特殊字符问题。
我正在使用以下命令
./psql -d data -U postgres -c "copy users from 'users.csv' delimiter E'\t' quote '~' csv"
它工作正常,直到它遇到一个带有“~”的字段,我将其用作引号值以不破坏现有的引号和引号等。
如何在 csv 文件 'Person~name' 中转义此字符,以便将其导入为 'Person~name'
CSV 规则列在 https://www.ietf.org/rfc/rfc4180.txt
中要在字符串中嵌入引号字符:
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"
在您的情况下,将双引号替换为波浪号,因为您已经选择了该分隔符。
示例:
test=> create table copytest(t text); CREATE TABLE test=> \copy copytest from stdin delimiter E'\t' quote '~' csv Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> ~foo~~bar~ >> \. test=> select * from copytest; t --------- foo~bar