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.

Advertisements

One thought on “Importing CSV into MySQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s