How to import data into MySql Database table from a CSV file

Whenever I have to fill a table with data in CSV file, I do this-

1.) Log in to your mysql console
    mysql -u root -p

2.) Make sure you have a database set up. now select that database
   mysql> use mydatabase;

3.) Create the table where you'll be inserting the data of CSV. now here, make sure that the table has the same column names ordering as they appear in the CSV file, because mysql puts all data as it encounters it. means in the same order. so if u break the order, then ur table may have fields with irrelevant data.

4.) Now fire this command to actually load the data from CSV into table-
     mysql>load data local infile 'c:/path/to ur/ file/filename.csv' into table yourTableName fields terminated by ',' enclosed by ' " ' lines terminated by '\n'


This should do all.
Any confusions?
ask me.
comment/compliment here.

0 comments: