加载文件中的数据,每行跳过1行

时间:2019-01-21 08:47:03

标签: php mysql load-data-infile

我正在尝试使用mysql的Load Data infile语法上传我的csv文件,但是在我的数据库中上传数据时,它仅加载一半的行,并且我注意到它的修补行。就像所有偶数只被插入而奇数没有被插入一样。

这是我的csv文件的示例格式:

storeid,txndate,productcategory1,qty,totalamt,uploaddate
"1100","19JAN2019","ADD ONS","1363","333.59","20JAN2019"
"1100","19JAN2019","KFC LP","58","1736.96","20JAN2019"
"1100","19JAN2019","KFC SP","269","1093.02","20JAN2019"
"1100","19JAN2019","LTO","26","495.39","20JAN2019"
"1100","19JAN2019","VALUE","71","534.13","20JAN2019"
"1102","19JAN2019","ADD ONS","244","32.5","20JAN2019"
"1102","19JAN2019","KFC LP","9","239.91","20JAN2019"
"1102","19JAN2019","KFC SP","70","277.63","20JAN2019"
"1102","19JAN2019","LTO","3","88.48","20JAN2019"
"1102","19JAN2019","VALUE","18","99.95","20JAN2019"
"1104","19JAN2019","ADD ONS","930","124.32","20JAN2019"
"1104","19JAN2019","KFC LP","21","680.79","20JAN2019"
"1104","19JAN2019","KFC SP","196","971.11","20JAN2019"
"1104","19JAN2019","LTO","17","338.84","20JAN2019"

这是我的加载数据文件脚本

$loadData = sprintf("
    LOAD DATA local INFILE '%s' IGNORE INTO TABLE prodmix 
    CHARACTER SET UTF8 FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' 
    LINES TERMINATED BY '\\n' IGNORE 1 LINES 
    (
        @storeid,
        @txndate,
        @productcategory1,
        @qty, 
        @totalamt, 
        @uploaddate
    )
    SET
    id=null, 
    storeid=@storeid, 
    txndate=@txndate, 
    productcategory1=@productcategory1,
    qty=@qty,
    totalamt=@totalamt,
    uploaddate=@uploaddate,
    unique_row=CONCAT(@txndate,'_',@storeid,'_',
    @productcategory1,'_',@qty,'_',@totalamt,'_',@uploaddate),
    created_at=now()
    ", addslashes($absolute_path));
    if(DB::connection()->getpdo()->exec($loadData)){
        //$total_success = $total_success +1;
        $response['status'] = 'success';                    
    }

可能导致其他所有行失败的原因。

1 个答案:

答案 0 :(得分:1)

您确定本地数据csv文件行的终止符是'\ n'而不是'\ r \ n'吗?