Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip error lines while loading data to mysql table from delimited file

Tags:

mysql

I am loading data from text file to mysql table using following query:

LOAD DATA INFILE "myFile.csv"
INTO TABLE some_table
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n' (admin,consumer);

But when my file contains some error lines it is not able skip that line and the whole process stops at that place. I mean if my file format like :

----------
sankr : kumar
----------
ramesh:rao
----------
new users add here
----------
sri : vennla
----------
anu : bhavya
----------

I have to load by skipping the line "new users add here". How can do this?

like image 349
sankar Avatar asked Dec 15 '11 17:12

sankar


People also ask

How do I ignore a line in MySQL?

The IGNORE number LINES clause can be used to ignore lines at the start of the file. For example, you can use IGNORE 1 LINES to skip an initial header line containing column names: LOAD DATA INFILE '/tmp/test.

Which statement can you use to load data from a file into a table MySQL?

mysql> LOAD DATA LOCAL INFILE '/path/pet. txt' INTO TABLE pet; If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead: mysql> LOAD DATA LOCAL INFILE '/path/pet.

How do I import a csv file into MySQL?

The following are steps that you want to import data into a table: Open table to which the data is loaded. Review the data, click Apply button. MySQL workbench will display a dialog “Apply SQL Script to Database”, click Apply button to insert data into the table.

Which is more efficient load data insert?

LOAD DATA (all forms) is more efficient than INSERT because it loads rows in bulk.


1 Answers

The keyword you're looking for is IGNORE.

As in:

LOAD DATA INFILE "myFile.csv" IGNORE
INTO TABLE some_table
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n' (admin,consumer);
like image 181
Dylan Avatar answered Oct 17 '22 19:10

Dylan