Thursday, June 28, 2012

importing csv file to MySQL database table


Sometimes we need to get the MySQL tables into an Excel sheet. How this can be done? We can dump the MySQL tables into a csv format file. It is explained in our previous post Dumping MySQL table into CSV file. But how to import a csv file to a mysql database table? We will discuss it in this post.

Login to your MySQL server.
[root@database ~]# mysql -p
Enter password:

Use the desired database
mysql> use database_name

For importing the csv file "viewership.csv" from the directory "/root/databases" into the table "table_name"

mysql> load data local infile '/root/databases/viewership.csv' into table  table_name fields terminated by ',' enclosed by '"' lines terminated by '\n';

Thats it. Now the csv file is loaded into the table.

how to dump MySQL table into CSV file

Recommended Reading

1. High Performance MySQL: Optimization, Backups, and Replication
2. MySQL (4th Edition)
3. MYSQL in a Nutshell (In a Nutshell (O'Reilly))

No comments:

Post a Comment

Be nice. That's all.