PHP Classes

handling of input rows that duplicate existing rows

Recommend this page to a friend!

      Quick CSV import  >  All threads  >  handling of input rows that...  >  (Un) Subscribe thread alerts  
Subject:handling of input rows that...
Summary:A little work-around
Messages:1
Author:Alexander Skakunov
Date:2007-12-18 15:58:00
 

  1. handling of input rows that...   Reply   Report abuse  
Picture of Alexander Skakunov Alexander Skakunov - 2007-12-18 15:58:00
According to MySQL manual (http://dev.mysql.com/doc/refman/5.0/en/load-data.html), you can control the process of replcing/ignoring of duplicated rows:

**************************
The REPLACE and IGNORE keywords control handling of input rows that duplicate existing rows on unique key values:

* If you specify REPLACE, input rows replace existing rows. In other words, rows that have the same value for a primary key or unique index as an existing row. See Section 11.2.6, “REPLACE Syntax”.

* If you specify IGNORE, input rows that duplicate an existing row on a unique key value are skipped. If you do not specify either option, the behavior depends on whether the LOCAL keyword is specified. Without LOCAL, an error occurs when a duplicate key value is found, and the rest of the text file is ignored. With LOCAL, the default behavior is the same as if IGNORE is specified; this is because the server has no way to stop transmission of the file in the middle of the operation.
**************************

To make this happen, find this code in the class file:

$sql = "LOAD DATA INFILE '".@mysql_escape_string($this->file_name).
"' INTO TABLE `".$this->table_name.

Add REPLACE or IGNORE keyword before INTO TABLE like this:

$sql = "LOAD DATA INFILE '".@mysql_escape_string($this->file_name).
"' REPLACE INTO TABLE `".$this->table_name.

Of course you should have unique keys on this table (or PRIMARY KEY).

P.S. I think I'll add such option to the next release of the class.