How to Import CSV data to a MySQL table in a database from command line?
September 14, 2011 at 7:33 pm Leave a comment
First log into MySQL from command line and create the table “table_user” under the database “db_user”
mysql> use db_user;
mysql> desc table_user;
+——-+————–+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+——-+————–+——+—–+———+—————-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| age | varchar(20) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
+——-+————–+——+—–+———+—————-+
4 rows in set (0.00 sec)
mysql> load data local infile ‘/userinfo.csv’
-> into table table_user
-> fields terminated by ‘,’
-> lines terminated by ‘\n’
-> (name, age, email);
Note: “id” field is skipped from the list for it to be auto incremented.
Check more information at kurinchilamp.kurinchilion.com
Entry filed under: Apache, MySQL. Tags: csv, import command line, MySQL.
Trackback this post | Subscribe to the comments via RSS Feed