public function loadDataLocalInfile($table, $columns, $fileName)
{
$filePath = Storage::disk('local')->path($fileName);
$fields = '';
foreach ($columns as $key => $column) {
$fields .= $column . ',';
}
$fields = rtrim($fields, ',');
DB::raw("
LOAD DATA LOCAL INFILE '" . $filePath . "'
INTO TABLE temporary_" . $table . "
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES (" . $fields . ")");
$updateColumns = '';
foreach ($columns as $key => $column) {
$updateColumns .= $column . ' = VALUES(' . $column . '),';
}
$updateColumns = rtrim($updateColumns, ',');
DB::raw('
INSERT INTO ' . $table . '
SELECT * FROM temporary_' . $table . '
ON DUPLICATE KEY UPDATE ' . $updateColumns);
}
执行脚本时没有错误,没有数据插入数据库,当我这样做时,所有表都存在
print_r(Storage::disk('local')->get($fileName));
已打印CSV文件
"name";"image"
"userNameA";"urlImageA"
"userNameB";"urlImageB"
"userNameC";"urlImageC"
我在config / database.php中的mysql选项中添加了PDO :: MYSQL_ATTR_LOCAL_INFILE => true