0

Trying to insert a CSV to a table via LOAD DATA LOCAL INFILE. It doesn't show errors but table is empty (CSV is not empty). There is a workaround doing foreaches and manual inserts but it's much slower compared to LOAD DATA INFILE.

Any suggestions?

$resource = Mage::getSingleton('core/resource');
$write = $resource->getConnection('core_write');

$filename = Mage::getBaseDir()."/var/mdb_processed/parts.csv";
$query = <<<eof
    LOAD DATA LOCAL INFILE '$filename'
 INTO TABLE ppi_parts
 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' lines terminated by '\n'
 ignore 1 lines
(partID,sku,isFreight,quantity,price,lastCost,vwCode,shortDesc,longDesc,headline,consumerDesc,salesMessage,relatedProd1,relatedProd2,weight,oversized,corePrice,titleMetaDescription,metaKeywords,fits,notes,seoField,location)
eof;


$write->query($query);
JoaquinLarra
  • 117
  • 4
  • 13

2 Answers2

0

Or the PDO version of this:

$options = array(PDO::MYSQL_ATTR_LOCAL_INFILE => true);
$local = simplexml_load_file(Mage::getBaseDir() . "/app/etc/local.xml");
$connection = $local->global->resources->default_setup->connection;
$pdoDb = new PDO(
    "mysql:host={$connection->host};dbname={$connection->dbname}", 
    $connection->username, 
    $connection->password, 
    $options
    );

$path_to_datafile = Mage::getBaseDir()."/var/mdb_processed/parts.csv";

$sql = "LOAD DATA LOCAL INFILE '{$path_to_datafile}'
        REPLACE INTO TABLE ppi_parts
        FIELDS TERMINATED BY ','
        OPTIONALLY ENCLOSED BY '\"' 
        LINES TERMINATED BY '\\n'
        IGNORE 1 LINES;";

$result = $pdoDb()->exec($sql);
Silas Palmer
  • 2,687
  • 1
  • 29
  • 30
-1

I resolved it by manually connecting to the DB. Varien DB object for some reason wasn't working.

To create a manual connection I had to do:

$local = simplexml_load_file( Mage::getBaseDir()."/app/etc/local.xml");

$connection = $local->global->resources->default_setup->connection;
$filename = Mage::getBaseDir()."/var/mdb_processed/parts.csv";

$link = mysql_connect($connection->host,$connection->username,$connection->password);
mysql_select_db($connection->dbname, $link) or die('Could not select database.');

Then:

$query = <<<eof
        LOAD DATA LOCAL INFILE '$filename'
     INTO TABLE ppi_parts
     FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' lines terminated by '\n'
     ignore 1 lines
    (partID,sku,isFreight,quantity,price,lastCost,vwCode,shortDesc,longDesc,headline,consumerDesc,salesMessage,relatedProd1,relatedProd2,weight,oversized,corePrice,titleMetaDescription,metaKeywords,fits,notes,seoField,location)
eof;
mysql_query($query);

Works perfectly.

JoaquinLarra
  • 117
  • 4
  • 13