从fputcsv加载数据本地填充:未记录数据

ibps3vxo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(340)

我有下面的csv文件:(3列)

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

我将其存储为文件: fputcsv($fp, array($data0, $data1, $data2)); 然后我想用以下查询将其存储在db中:

$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   | ""   |
+--+----------------+-------------+--------+------+

我尝试实现的是:相应地存储所有数据
编辑:我的表定义:

/*Table structure for table `tabel_name` */

DROP TABLE IF EXISTS `tabel_name`;

CREATE TABLE `tabel_name` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `col0` VARCHAR(16) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `col1` VARCHAR(12) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `colxx` VARCHAR(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `col2` VARCHAR(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` TIMESTAMP NULL DEFAULT NULL,
  `updated_at` TIMESTAMP NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tabel_col0_unique` (`col0`),
  UNIQUE KEY `tabel_col1_unique` (`col1`)
) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
wpx232ag

wpx232ag1#

您已将col1定义为unique:

UNIQUE KEY `tabel_col1_unique` (`col1`)

并且您的数据具有非唯一值:

998877665544331,,baba
998877665544332,,

所以第一行被插入,但是第二行由于惟一约束而失败。

相关问题