首页  >  文章  >  数据库  >  导入数据到mysql的一种简单的方法_MySQL

导入数据到mysql的一种简单的方法_MySQL

WBOY
WBOY原创
2016-06-01 12:58:401081浏览

因为ubuntu默认自带的mysql版本为5.5,并不能使用load data infile这种高级的功能,因此我们写了一个通用的脚本来上传文件

shell脚本

 

cat ./employee.csv | while read LINE                                                                             
do                                                                                                               
        eval $( echo $LINE | awk -F ',' '{print "ds="$1 ";id="$2 ";name="$3}' )                                  
        echo ds=$ds id=$id name=$name                                                                            
        mysql -uroot -p655453 test --default-character-set=utf8 -e  "replace into wechat_employee values('$ds','$id','$name')"
done   


 

要导入数据到Mysql最重要的一点就是要三码合一,即client和server的编码还有表的编码要一致,server的编码可以在/etc/mysql找一下配置文件修改,客户端的编码在insert之前可以设置一下,可以忽略

 

show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

查看表的编码,不是utf8的话要修改

 

 

mysql> show create table wechat_employee;
+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table           | Create Table                                                                                                                                                                             |
+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| wechat_employee | CREATE TABLE `wechat_employee` (
  `ds` varchar(20) DEFAULT NULL,
  `femployeeid` int(20) DEFAULT NULL,
  `femployeename` varchar(256) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

接着导入,大功告成

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn