Abdus Sattar Bhuiyan
Abdus Sattar Bhuiyan

Reputation: 3074

load data infile is not working

I am trying to batch upload by csv file. my csv file located in webroot/CSV/data.csv. Data are as follows:

name,email
santo, [email protected]

my code:

$sql = "LOAD DATA INFILE '$filepath'
    INTO TABLE customers
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '\"' 
    LINES TERMINATED BY ',,,\\r\\n'
    IGNORE 1 LINES 
    (name, email)";
    $this->Customer->query($sql);

here

$filepath = WWW_ROOT . 'CSV' . DS.'data.csv';

Getting error:

C:\server\mysql\data\serverhtdocsdemo-home25appwebrootCSVdata.csv' not found (Errcode: 2 "No such file or directory")

I check the file it exists and file location in sql seems to okay:

SQL Query: LOAD DATA INFILE 'C:\server\htdocs\demo-home25\app\webroot\CSV\data.csv' INTO TABLE customers FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES (name, email)

What is the wrong in my code?

Upvotes: 0

Views: 1862

Answers (2)

user2925795
user2925795

Reputation: 398

You tried to use "LOAD DATA LOCAL INFILE"?

LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3, col4, col5...);

In your case:

$sql = "LOAD DATA LOCAL INFILE '$filepath'
    INTO TABLE customers
    FIELDS TERMINATED BY ','
    OPTIONALLY ENCLOSED BY '\"' 
    LINES TERMINATED BY ',,,\\r\\n'
    IGNORE 1 LINES 
    (name, email)";
    $this->Customer->query($sql);

"If LOCAL is specified, the file is read by the client program on the client host and sent to the server." from Documentation

Answered in https://stackoverflow.com/a/14133740/2925795

Upvotes: 2

Omar Alves
Omar Alves

Reputation: 763

I had the same issue. Sometimes it happens that your mysql server and client understand file locations differently.

Add LOCAL modifier (as LOAD DATA LOCAL INFILE) to your query

$sql = "LOAD DATA LOCAL INFILE '$filepath'
INTO TABLE customers
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"' 
LINES TERMINATED BY ',,,\\r\\n'
IGNORE 1 LINES 
(name, email)";
$this->Customer->query($sql);

Upvotes: 0

Related Questions