postgresql数据库的COPY命令的简单用法
(2010-10-17 18:04)
我测试的表结构如下
mydb=# \d t2
资料表 "public.t2"
栏位 | 型别 | 修饰词
------+-----------------------+--------
id | character varying(10) |
name | character varying(15) |
test | boolean |
v1 | numeric(8,0) |
v2 | numeric(8,0) |
导入命令如下
mydb=# copy t2 from 'd:/a.txt' USING delimiters ',';
命令中的逗号为文件的分割符
a.txt内容如下
"1 ","snow ",True,25,10000
"2 ","snow1 ",True,18,10000
"3 ","snow2 ",False,25,100
"4 ","snow3 ",True,25,1
"5 ","snow4 ",True,25,10
导入到数据库后如下
mydb=# select * from t2;
id | name | test | v1 | v2
----------+--------------+------+----+-------
"1 " | "snow " | t | 25 | 10000
"2 " | "snow1 " | t | 18 | 10000
"3 " | "snow2 " | f | 25 | 100
"4 " | "snow3 " | t | 25 | 1
"5 " | "snow4 " | t | 25 | 10
"1 " | "snow " | t | 25 | 10000
"2 " | "snow1 " | t | 18 | 10000
"3 " | "snow2 " | f | 25 | 100
"4 " | "snow3 " | t | 25 | 1
"5 " | "snow4 " | t | 25 | 10
(10 笔资料列)
mydb=# \d t2
资料表 "public.t2"
栏位 | 型别 | 修饰词
------+-----------------------+--------
id | character varying(10) |
name | character varying(15) |
test | boolean |
v1 | numeric(8,0) |
v2 | numeric(8,0) |
导入命令如下
mydb=# copy t2 from 'd:/a.txt' USING delimiters ',';
命令中的逗号为文件的分割符
a.txt内容如下
"1 ","snow ",True,25,10000
"2 ","snow1 ",True,18,10000
"3 ","snow2 ",False,25,100
"4 ","snow3 ",True,25,1
"5 ","snow4 ",True,25,10
导入到数据库后如下
mydb=# select * from t2;
id | name | test | v1 | v2
----------+--------------+------+----+-------
"1 " | "snow " | t | 25 | 10000
"2 " | "snow1 " | t | 18 | 10000
"3 " | "snow2 " | f | 25 | 100
"4 " | "snow3 " | t | 25 | 1
"5 " | "snow4 " | t | 25 | 10
"1 " | "snow " | t | 25 | 10000
"2 " | "snow1 " | t | 18 | 10000
"3 " | "snow2 " | f | 25 | 100
"4 " | "snow3 " | t | 25 | 1
"5 " | "snow4 " | t | 25 | 10
(10 笔资料列)
如果COPY时提示:
Use the escape string syntax for escapes, e.g., E'\r\n'.
Use the escape string syntax for escapes, e.g., E'\r\n'.
可使用下面的方法运行COPY
copy dxha from E'E:\\dxha\\2fgf_kzm.txt' USING DELIMITERS ',';

