mysql数据库导出csv文件命令:
mysql> select first_name,last_name,email from account into outfile 'e://output1.csv' fields terminated by ','optionally enclosed by ''lines terminated by '/n';
csv文件效果:
sunny grigoryan lovechoosesun@gmail.com
jon siegal sun@riliantech.net
joe siegal zhao@gmail.com
alejandro medina wei@gmail.com
cvs文件导入mysql数据库命令:
mysql> load data local infile 'e://input1.csv' into table test1 fields termin
ated by ','lines terminated by '/n'(first_name,last_name,email);
query ok, 1 row affected, 1 warning (0.00 sec)
records: 69 deleted: 0 skipped: 68 warnings: 0
mysql> select * from test1;
+----+------------+-----------+--------------------------+
| id | first_name | last_name | email |
+----+------------+-----------+--------------------------+
| 0 | sunny | grigoryan | lovechoosesun@gmail.com
+----+------------+-----------+--------------------------+
fields terminated by ---- 字段终止字符
optionally enclosed by ---- 封套符
lines terminated by ---- 行终止符
通过mysql客户端shell连接到服务器,选择使用的数据库,输入sql代码:
select * from test_info
into outfile '/tmp/test.csv'
fields terminated by ',' optionally enclosed by '' escaped by ''
lines terminated by '/r/n';
里面最关键的部分就是格式参数
这个参数是根据rfc4180文档设置的,该文档全称common format and mime type for comma-separated values (csv) files,其中详细描述了csv格式,其要点包括:
(1)字段之间以逗号分隔,数据行之间以/r/n分隔;
(2)字符串以半角双引号包围,字符串本身的双引号用两个双引号表示。
通过执行上述代码,便可以将需要的数据以csv格式导出到执行的文件中。
另外,mysql中导入csv格式数据的sql代码如下:
load data infile '/tmp/test.csv'
into table test_info
fields terminated by ',' optionally enclosed by '' escaped by ''
lines terminated by '/r/n';
当csv文件中的每一行记录的列数小于数据库表时,以下语句将十分有用:
load data infile /tmp/appleappv2_search_day_20110525.csv into table apple_search_log fields terminated by ';' lines terminated by '/n'(apptypeid,keyword,searchtime,model) ;
蓝色字部分是亮点。如果数据表包含自增字段,例如自增的id, 这个语句将十分有用。
转自:http://www.cnblogs.com/zeroone/archive/2013/01/12/2857388.html