Avatar
Fernando Vásquez is an electronics engineer and software developer currently based in the world. He occasionally blogs about Python and Android programming.

Import CSV into MySQL (MariaDB)

To load data from a CSV file you can just execute the following SQL command:

LOAD DATA INFILE "/home/fer/data.csv"
	INTO TABLE __TABLE__
	COLUMNS TERMINATED BY ','
	OPTIONALLY ENCLOSED BY '"'
	ESCAPED BY '"'
	LINES TERMINATED BY '\n'
	IGNORE 1 LINES;

Additionally, you can add IGNORE statement after filename to ignore errors in rows:

LOAD DATA INFILE "/home/fer/data.csv" IGNORE
	...

In the case you are running MySQL server with --secure-file-priv option, you will get an error. To check it, you can execute the following query:

SHOW VARIABLES LIKE "secure_file_priv";

Then you have to move your csv file into the directory shown in secure-file-priv, otherwise, you have to restart MySQL with secure-file-priv disabled.

all tags