gpt4 book ai didi

php - 从 fputcsv : data not recorded 加载数据本地 INFILE

转载 作者:行者123 更新时间:2023-11-29 05:53:23 25 4
gpt4 key购买 nike

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

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

我通过以下方式将其存储为文件:fputcsv($fp, array($data0, $data1, $data2));

然后我想用这个查询将它存储在数据库中:

$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);

问题/疑问:

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

    +--+----------------+-------------+--------+------+
    |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;

最佳答案

您已将 col1 定义为唯一的:

  UNIQUE KEY `tabel_col1_unique` (`col1`)

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

998877665544331,,baba
998877665544332,,

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

关于php - 从 fputcsv : data not recorded 加载数据本地 INFILE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52061724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com