1.现在有一个data.sql 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Use DB_NAME;
INSERT INTO `tbl_province` VALUES ('1', '北京');
INSERT INTO `tbl_province` VALUES ('2', '上海');
INSERT INTO `tbl_province` VALUES ('3', '天津');
INSERT INTO `tbl_province` VALUES ('4', '重庆');
INSERT INTO `tbl_province` VALUES ('5', '河北');
INSERT INTO `tbl_province` VALUES ('6', '山西');
INSERT INTO `tbl_province` VALUES ('7', '内蒙古');
INSERT INTO `tbl_province` VALUES ('8', '辽宁');
INSERT INTO `tbl_province` VALUES ('9', '吉林');
INSERT INTO `tbl_province` VALUES ('10', '黑龙江');
...
...

如果直接插入一个utf8的数据表中将出现中文无法正常插入;

1
mysql < data.sql

2.故需要将data.sql从gb2312转化为utf8,才可以正常导入数据库

1
# iconv -f gb2312 -t utf8 data.sql -o data_utf8.sql

转化后的data_utf8.sql内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
USE DB_NAME;
INSERT INTO `tbl_province` VALUES (1, '鍖椾含');
INSERT INTO `tbl_province` VALUES (2, '涓婃捣');
INSERT INTO `tbl_province` VALUES (3, '澶╂触');
INSERT INTO `tbl_province` VALUES (4, '閲嶅簡');
INSERT INTO `tbl_province` VALUES (5, '娌冲寳');
INSERT INTO `tbl_province` VALUES (6, '灞辫タ');
INSERT INTO `tbl_province` VALUES (7, '鍐呰挋鍙?);
INSERT INTO `tbl_province` VALUES (8, '杈藉畞');
INSERT INTO `tbl_province` VALUES (9, '鍚夋灄');
INSERT INTO `tbl_province` VALUES (10, '榛戦緳姹?);
...
...

3.将转化后的data_utf8.sql导入数据库

1
mysql < data_utf8.sql

4.导入成功 :D