Uncategorized

Import CSV file into MySQL

Today, I had to import a CSV file into a MySQL database. I thought, this would be a quick snap with phpMyAdmin and was surprised to NOT see a file import, only SQL style imports are supported with phpMyAdmin. Okay, so off we go to CLI MySQL commands (or SQL execution, but since the file was on the linux system anyway, I thought CLI would be easier).

I’m mainly putting this here, so I have a quick reference here.


mysql -u root -p
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> LOAD DATA INFILE '/tmp/CSV.txt'
-> INTO TABLE tblTest
-> FIELDS TERMINATED BY ','
-> LINES TERMINATED BY '\r\n';
Query OK, 2 rows affected (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0

Simple as that!

Today, I had to import a CSV file into a MySQL database. I thought, this would be a quick snap with phpMyAdmin and was surprised to NOT see a file import, only SQL style imports are supported with phpMyAdmin. Okay, so off we go to CLI MySQL commands (or SQL execution, but since the file was on the linux system anyway, I thought CLI would be easier).

I’m mainly putting this here, so I have a quick reference here.


mysql -u root -p
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> LOAD DATA INFILE '/tmp/CSV.txt'
-> INTO TABLE tblTest
-> FIELDS TERMINATED BY ','
-> LINES TERMINATED BY '\r\n';
Query OK, 2 rows affected (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0

Simple as that!