A Simple Method for importing data to mysql.

Source: Internet
Author: User

A Simple Method for importing data to mysql.

Because ubuntu comes with mysql 5.5 by default and cannot use the advanced feature load data infile, we have written a general script to upload files.


Shell script

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   


The most important thing to import data to Mysql is to combine three codes, that is, the client and server encoding and table encoding must be consistent, server encoding can be found in/etc/mysql to modify the configuration file. The client encoding can be set before insert, which can be ignored.


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/ |+--------------------------+----------------------------+

View the table encoding. If it is not utf8, modify it.

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)

Next, import.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.