Importing CSV into MySQL

I’ve wasted abit of time doing this today…

1. Create your database

>mysqladmin create dbname

2. Create your table with the right fields. (I usually use an Admin tool like phpmyadmin to do this).

3. You need to login to your mysql console

>mysql -u root
>use dbname;
>LOAD DATA LOCAL INFILE 'path_to_my_file.csv' INTO TABLE tablename
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\r\\n'
OPTIONALLY ENCLOSED BY """" (field1,field2,field3);

4. Play around with the TERMINATED BY setting and OPTIONAL setting for your own need.


  1. RavCai-16

    Thanks for Sharing..

Leave a Comment




I am the founder of RoRCraft Ltd., a web consultancy firm that develops usable and speedy web applications for our clients.


Based in Sydney, I travel to Hong Kong and HangZhou China to meet with my team regularly. I love exploring new technologies and business ideas that helps making our world a better place.



An online video conversion tool. It allows everyone to freely convert their videos for ipods, mobiles and flash.


Rails Job


Job board for Ruby and Rails developers.


View Rex Chung's profile on LinkedIn

www.flickr.com

LATEST RUBY JOBS

Embed this on your site?