|
我有一个csv文件,其内容如下:
" n3 m8 m+ S# H" ^% j10,53073,0,0,'Y','2008-05-30 21:46:55',0,'2008-05-30 21:48:04',0,53071,2
2 u7 n4 C$ |' T7 V我想将csv数据加载到my_table中。' Q/ l1 W3 e: v- P
CREATE TABLE my_table9 h- G% `, R2 P* r1 V
(
* `+ }# w) i+ K: o% f2 N+ X+ @! p9 O ad_tree_id numeric(10,0) NOT NULL,9 `/ S% V! D& H- }/ P# N
node_id numeric(10,0) NOT NULL,
' V7 p) m2 Q+ K ad_client_id numeric(10,0) NOT NULL,, r5 K! ?7 u# {. Q# w3 i
ad_org_id numeric(10,0) NOT NULL,0 h' \/ L7 H; o4 h3 C( H
isactive character(1) NOT NULL DEFAULT 'Y'::bpchar,, e5 o; v5 ^6 F2 a
created timestamp without time zone NOT NULL DEFAULT now(),% c( J" G3 J! J: k+ ^7 [1 m
createdby numeric(10,0) NOT NULL,0 e; N7 J9 o- t I; d3 x+ Y
updated timestamp without time zone NOT NULL DEFAULT now(),: ^7 D5 e5 [' E
updatedby numeric(10,0) NOT NULL,
6 x [. V' l" P% `$ K6 C* D parent_id numeric(10,0),3 G* ]$ f* \5 v( O P
seqno numeric(10,0),7 P$ s! E; {6 Y0 y; A# o. E
CONSTRAINT ad_treenodemm_pkey PRIMARY KEY (ad_tree_id , node_id ),$ \. ~% Q4 W1 H# K; T1 g, \! F
CONSTRAINT adtree_adtreenodemm FOREIGN KEY (ad_tree_id)+ w% C, f/ o' d+ \1 u
REFERENCES ad_tree (ad_tree_id) MATCH SIMPLE5 l# \5 F) o& E/ R" O
ON UPDATE NO ACTION ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,+ q" G) C# e. T8 ~
CONSTRAINT ad_treenodemm_isactive_check CHECK (isactive = ANY (ARRAY['Y'::bpchar, 'N'::bpchar])), n" z9 o1 y. Z& U' J9 v
)
' b Q8 L5 e U8 X" i* \当我在pgAdmin III工具中运行此命令时:
: D7 ]6 N2 A9 W. [! X% k- I6 X% G* GCOPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV;5 I! F! f F; M7 ?' S5 { H; r3 g
我收到错误消息:" A* T) X* J7 p0 t+ ] @. n
ERROR: value too long for type character(1)
h6 G' [4 O; O) ]+ V& TCONTEXT: COPY my_table, line 1, column isactive: "'Y'"4 b7 f* {6 `* i/ l+ i. Q; e$ W
然后我修改了这样的命令:
8 S" b- D, a3 O/ MCOPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE ''';
M: ^. y5 B4 C2 c) L" vCOPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '\'';5 }: u. A% |1 Y* S7 i$ E3 s# M& O$ n
COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '\'' ESCAPE '\';
: \! }4 K8 ?" V! w: M. l6 jCOPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '\'' ESCAPE \;
# p! w- T/ q0 M; d尝试全部失败。
: W, A% V1 I' f+ `3 ^( @5 R那么,任何人都可以在这种情况下向我显示正确的COPY命令?
5 m/ J2 k! r6 P2 J5 o. i2 C
* S. K0 s5 ]" b" u9 r解决方案:
" v# h( N* t* ~6 R' @ ( S. `# p: @" u) Q( @' d
* z9 U$ w7 F' i
3 W( O& c. x% r" A1 _# ~ 双引号(如果standard_conforming_strings已启用,请参阅docs)
& }+ Z! s1 u3 kCOPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE '''';4 h' R9 [% m# B; {2 t
或使用非标准的PostgreSQL专用转义字符串:; |2 p; }; b8 v7 H% M- l
COPY my_table FROM 'c:\downloads\file.csv' DELIMITERS ',' CSV QUOTE E'\''; |
|