Importing and Exporting data to CSV from Mysql using command line

Everyone knows you can use phpmyadmin to import and export data to and from Mysql. I needed to be able to do this programaitcally and fast. I use Drupal so could have written a module to read the data from the table and then write out to a file line by line, this would be slow and my tables are large. So I found this method which is native MYSQL and is really efficent.

  SELECT columnname,anothercoulmnname INTO OUTFILE filename.csv FIELDS TERMINATED BY '|' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM tablename;
This will dump out a file to the mysql folder holding the datafiles. You can supply a path at filename.csv i.v. /home/me/files/filename.csv to send it to a specific destination but you will have to make sure that MYSQL has privileges to write to the directory. 

Your rating: None

Comments

Load data in from a csv file

To do the opposite of this (load data from a csv file) see an explanation here