Programming
Load CSV data into mysql
July 21, 2012
0

Hey folks,

two posts in a day!!! Anyway this is a small tip on how to upload a CSV file in mysql using the command line.

First  you need to enable an option on your mysql while accessing it in the usual way :

mysql -u "myuser" -p"mypass" --local-infile=on

This prevents mysql to raise security errors  and let’s you load  the file (about security error read this on the mysql documentation ).

Now select the target database:

use mydatabase;

And then load your file using:

LOAD DATA LOCAL INFILE '/home/user/myfile.csv'  INTO TABLE mytable  FIELDS TERMINATED BY ','  LINES TERMINATED BY '\r\n';

Remember that if you create your CSV file in a Windows environment lines are terminated by ‘\r\n’, while in a Unix environment lines are terminated by ‘\n’ .

Enjoy!

Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close