How To Export and Import data from csv into Mysql

How To Export and Import of csv data out of and into MySQL

This method works for the MySQL 5  and not sure about other versions.

Exporting the MySQL data into a comma delimited (.csv) file

SELECT * INTO OUTFILE ‘ /data.csv’ FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’ FROM table1;

This will dump your table into a file with each row from the database being on it’s own line, columns separated by commas, and if there are any spaces or funny business going on with the data in a field, the data will be enclosed in double quotes. You can make changes to this if you like by, for instance, substituting ‘,’ with ‘\t’ would result in a tab delimited file.

Importing the comma delimited (.csv) file back into MySQL

First thing, you’ll need to clear out your old table (otherwise the keys would collide):

DELETE FROM table;

Then you can proceed with the import:

LOAD DATA INFILE ‘ /data.csv’ INTO TABLE table1 FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’;

Share

No related posts.

Tags: , , , , , , , , ,
Technical, , , , , , , , , Permalink

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>