LOAD DATA LOCAL, How do I skip the first line?
Asked Answered
M

3

72

I'm trying to Load a CSV file into my MySQL database, But I would like to skip the first line.

I fact It contains the name of my columns and no interesting data.

Here is the query I'm using:

LOAD DATA LOCAL INFILE '/myfile.csv' 
INTO TABLE tableName
FIELDS TERMINATED BY ','
ENCLOSED BY '\"' 
LINES TERMINATED BY '\n' 
(column,column,column);
Manet answered 24/10, 2009 at 15:59 Comment(0)
I
119
LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES;

(reference)

Indifferent answered 24/10, 2009 at 16:9 Comment(2)
I have a question. Lets take IGNORE LINES 5; as an example. Would that ignore line 5 or lines 1-5?Kilocalorie
@EatSleepCode, it will ignore the first N lines, so lines 1 to 5, this last included.Lignite
O
46

For those curious, IGNORE N LINES should be after the separator qualifiers:

LOAD DATA LOCAL INFILE '/myfile.csv' 
INTO TABLE tableName
FIELDS TERMINATED BY ','
ENCLOSED BY '\"' 
LINES TERMINATED BY '\n' 
IGNORE 1 LINES
(column,column,column);
Oidium answered 2/8, 2016 at 20:15 Comment(1)
Your note on the placement of IGNORE is KEY!Steeve
P
2

Try this:

IGNORE N LINES
LOAD DATA INFILE "/path/to/file.csv"
INTO TABLE MYTABLE 
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
Pontoon answered 28/11, 2019 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.