csv to mysql code example

Example 1: mysql load data infile csv

LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3, col4, col5...);

Example 2: mysqlimport csv

C:\mysql\bin>mysqlimport --ignore-lines=1 --fields-terminated-by=, --verbose --local -u root query C:/mysql/bin/mysql-files/address.csv

Connecting to localhost

Selecting database query

Loading data from LOCAL file: C:/mysql/bin/mysql-files/address.csv into address

query.address: Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
Disconnecting from localhost

mysql> Select * from Address;
+---------+----------+-------------+
| Name    | LastName | Address     |
+---------+----------+-------------+
| Mohan   | Sharma   | Sundernagar |
| Saurabh | Arora    | Chandigarh  |
| Rajesh  | Singh    | Lucknow     |
+---------+----------+-------------+
3 rows in set (0.00 sec)

Example 3: how to dump .csv file into mysql

LOAD DATA LOCAL INFILE 'c:/country.csv' 
INTO TABLE country 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

Tags:

Sql Example