导入数据到mysql的一种简单的方法,导入数据mysql
因为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)
接着导入,大功告成
版权声明:本文为博主原创文章,未经博主允许不得转载。
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。