Friday, June 08, 2007

How to import csv file directly to MySQL

In some situation we may need to import some huge CSV file directly to the SQL db rather converting them to SQL statements. As an example, in my case(project-ABC), It needed to insert large no of questions to the MySQL database which was saved as a csv file.

Command:
mysqlimport --fields-terminated-by=","
--fields-optionally-enclosed-by="\"" --lines-terminated-by="\n" -u user -ppassword

**Use your username & password
**give your csv file name

There is another way to do this too.
You can use command given (sample) from command line.
LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;


http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html

No comments: