0

What I was trying to do is to load a csv file into the database. But I am having a problem because of duplicate entry.

I am using PHP script to load the file.

$sql = "LOAD DATA INFILE '" . $target_path;
        $sql.= "' INTO TABLE dummy ";   
        $sql.= "COLUMNS TERMINATED BY ','";
        $sql.= "OPTIONALLY ENCLOSED BY '\"'";
        $sql.= "ESCAPED BY '\"'";
        $sql.= "LINES TERMINATED BY '\n'";
        $sql.= "IGNORE 1 LINES;";

Database Table Structure:

id | firstname | lastname

CSV File content..

id | firstname | lastname

1  John Doe
2  James Carry
3  Luke Borrow

As expected 'id' is the primary key. Is there a way to skip values that violates primary key constraint on the fly?

1
  • 2
    @skrilled: there is no ON DUPLICATE KEY syntax for LOAD DATA INFILE, it's not a regular INSERT query. Even on INSERT queries, you could just use INSERT IGNORE if you don't plan to update anything. Commented Aug 13, 2013 at 23:00

1 Answer 1

6
LOAD DATA INFILE 'somefile' IGNORE 

From the manual:

If you specify IGNORE, input rows that duplicate an existing row on a unique key value are skipped.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.