Home > MySQL, PHP, Tips & Tricks > How to import a csv file into mysql from the prompt

How to import a csv file into mysql from the prompt

January 11th, 2007

For the follows out there who do not know how to import coma separated data into mysql from the prompt, here it is:

Assume:

PATH is the path to the csv file (can be relative to the directory you were in before logging into mysql)
TABLE is the table name that you have already created and whos fields match the csv file fields.
USERNAME is the mysql user that have access to insert data into TABLE
DATABASE is the database containing that table

- login to mysql by typing: mysql -u USERNAME -p DATABASE
The prompt will ask you for the user password, type it.

- Execute the following query:

load data local infile ‘PATH’ into table TABLE fields terminated by ‘,’ lines terminated by ‘\n’;

And voila.

MySQL, PHP, Tips & Tricks

  1. No comments yet.
  1. No trackbacks yet.