我正在尝试使用“复制”命令将 csv 文件导入到我的 postgres 表中。包含逗号的值已被双引号括起来,但我似乎找不到使用“复制”命令将它们加载到 postgres 表中而不会出现任何错误的选项。
我正在使用的“COPY”命令:
CREATE TABLE candidates (Sno int, name varchar, cases int, case_details varchar, .....);
copy candidates from 'something.csv' with NULL AS ' ' csv ;
违规 csv 行示例:
1, "some name", 2, "(1):IPC Sections - 147, 323, 352, 504, 506 , Other Details - Case no.283A/2000, A.C.J.M-5, Ghumangunj Ellahabad, UP, Dt.12.11.2000", .....
case_details
属性的值中包含逗号。那是我的问题。
最佳答案
适用于我(PG 9.2,linux):
$ cat something.csv
1, "some name", 2, "(1):IPC Sections - 147, 323, 352, 504, 506 , Other Details - Case no.283A/2000, A.C.J.M-5, Ghumangunj Ellahabad, UP, Dt.12.11.2000"
$ psql test
test=> CREATE TABLE candidates (Sno int, name varchar, cases int, case_details varchar);
CREATE TABLE
test=> \copy candidates from 'something.csv' with NULL AS ' ' csv ;
test=> select * from candidates ;
sno | name | cases | case_details
-----+------------+-------+--------------------------------------------------------------------------------------------------------------------------------------
1 | some name | 2 | (1):IPC Sections - 147, 323, 352, 504, 506 , Other Details - Case no.283A/2000, A.C.J.M-5, Ghumangunj Ellahabad, UP, Dt.12.11.2000
(1 row)
关于database - 将表导入 PostgreSQL 时如何转义 CSV 值中的逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505975/