Powered By

How do I import csv files into my database

Tags : | |


In some cases you may need to import the csv files to your database.
If you are having excel file just 

  1. Using the LOAD DATA INFILE SQL statement
First you need to login to the mysql command prompt and you need to type the following commands to import


To import the datafile, first upload it to your home directory, so that the file is now located at /importfile.csv on our local system. 


Then you type the following SQL at the mysql prompt:



mysql >  

mysql > LOAD DATA LOCAL INFILE '/importfile.csv' INTO TABLE test_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1, filed2, field3);



The above SQL statement tells the MySQL server to find your INFILE on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, and to put it into your MySQL test_table as columns field1, field2, and field3 respectively. 


  1. Using a script to parse and import the file


If you need to take a csv or other delimited data file and import it
INTO MySQL, you can write your own script to do it






Related Posts by Categories