从fputcsv加载数据本地文件:未记录数据

时间:2018-08-28 15:41:03

标签: php mysql csv load-data-infile fputcsv

我有以下CSV文件:(3列)

Button

我通过998877665544331,,baba 998877665544332,, 998877665544333,, 998877665544334,,test 998877665544335,, ,R32AS00ZZYY, 998877665544337,, 998877665544338,, 998877665544339,,asfd 998877665544330,, ,R32AS00XXYY, ,R32AS00XXZZ,

将其存储为文件

然后我要使用以下查询将其存储在数据库中:

fputcsv($fp, array($data0, $data1, $data2));

问题/问题:

  1. 为什么只存储第一条记录和第六条记录? 如下所示:

    $query = sprintf(
            "LOAD DATA LOCAL INFILE '%s' 
            INTO TABLE devices 
            FIELDS TERMINATED BY ','
            OPTIONALLY ENCLOSED BY '\"' 
            LINES TERMINATED BY '\\n'
            (col0, col1, col2);", addslashes(storage_path('app/file.csv')));
    
    $asdf = DB::connection()->getpdo()->exec($query);
    

我要实现的目标:相应地存储所有数据

编辑:我的表定义:

+--+----------------+-------------+--------+------+
|id| col0           | col1        | col xx | col2 |
+--+----------------+-------------+--------+------+
|01|998877665544331 | ""          | NULL   | baba |
|02| ""             | R32AS00ZZYY | NULL   | ""   |
+--+----------------+-------------+--------+------+

1 个答案:

答案 0 :(得分:2)

您已将col1定义为唯一:

  UNIQUE KEY `tabel_col1_unique` (`col1`)

您的数据具有非唯一值:

998877665544331,,baba
998877665544332,,

因此插入了第一行,但是由于唯一约束,第二行失败了。