I have below CSV file: (3 columns)
998877665544331,,baba
998877665544332,,
998877665544333,,
998877665544334,,test
998877665544335,,
,R32AS00ZZYY,
998877665544337,,
998877665544338,,
998877665544339,,asfd
998877665544330,,
,R32AS00XXYY,
,R32AS00XXZZ,
I store it as file by: fputcsv($fp, array($data0, $data1, $data2));
then i want to store it in DB with this query:
$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);
problem/question:
why it only store first record and 6th record? like below:
+--+----------------+-------------+--------+------+
|id| col0 | col1 | col xx | col2 |
+--+----------------+-------------+--------+------+
|01|998877665544331 | "" | NULL | baba |
|02| "" | R32AS00ZZYY | NULL | "" |
+--+----------------+-------------+--------+------+
what I try to achieve: stored all of data accordingly
EDIT: my table definition:
/*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;
You've defined col1 as unique:
UNIQUE KEY `tabel_col1_unique` (`col1`)
And your data has non-unique values:
998877665544331,,baba
998877665544332,,
So the first row gets inserted, but then the second fails because of the unique constraint.