Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用\copy命令将CSV导入Postgres时如何转义\n(换行符)?_Sql_Database_Postgresql_Csv_Escaping - Fatal编程技术网

Sql 使用\copy命令将CSV导入Postgres时如何转义\n(换行符)?

Sql 使用\copy命令将CSV导入Postgres时如何转义\n(换行符)?,sql,database,postgresql,csv,escaping,Sql,Database,Postgresql,Csv,Escaping,我正在尝试使用\copy from CSV文件将数据导入我的postgres表 数据如下: 1,2,"First Line \n Second Line", "Some other data" 我的目的是在导入数据时保留“\n” 以下工作: insert into table (col1, col2, col3, col4) values (1, 2, E'First Line \n Second Line', 'Some other data') 但是如何使用\copy命令获得相同的结果

我正在尝试使用\copy from CSV文件将数据导入我的postgres表 数据如下:

1,2,"First Line \n Second Line", "Some other data"
我的目的是在导入数据时保留“\n”

以下工作:

insert into table (col1, col2, col3, col4) values (1, 2, E'First Line \n Second Line', 'Some other data')

但是如何使用\copy命令获得相同的结果

在CSV中唯一可以转义的是字段分隔符(
”,默认情况下),可以通过将其加倍来转义

换行符不能转义,事实上也没有必要,因为您可以只使用文字换行符

这是CSV中的一行:

1,2,"First Line
Second Line", "Some other data"

恐怕您必须将csv修改为要保留的反斜杠
\n

b=# copy t from stdout;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> "First Line \n Second Line"
>> "First Line \\n Second Line"
>> \.
COPY 2
b=# select * from t;
              a
-----------------------------
 "First Line                +
  Second Line"
 "First Line \n Second Line"
(2 rows)

我有点担心最后我必须这么做。/只是一个小的辅助帮助,如何用换行符将Excel数据导出到csv?我想提供帮助,但我不知道Excel。您可以始终使用
COPY…FROM PROGRAM“command”
并替换一个为您处理csv文件的命令。