0

我正在尝试将 csv 导入数据库中的 mysql 表,但我不断收到此错误:

'字段列表'中的未知列''

INSERT INTO `job_budget_report` (`Job_Num`, `Description`, `EngWeekly`, `EngTTD`, `EngBudget`, `DesignWeekly`, `DesignTTD`, `DesignBudget`, `DetailsWeekly`, `DetailsTTD`, `DetailsBudget`, `EjectionWeekly`, `EjectionTTD`, `EjectionBudget`, `CarbonEDMWeekly`, `CarbonEDMTTD`, `CarbonEDMBudget`, `GdrillBMWeekly`, `GdrillBMTTD`, `GdrillBMBudget`, `CNCWeekly`, `CNCTTD`, `CNCBudget`, `MMQCOtherWeekly`, `MMQCOtherTTD`, `MMQCOtherBudget`, `SpottingWeekly`, `SpottingTTD`, `SpottingBudget`, `SpotPLWeekly`, `SpotPLTTD`, `SpotPLBudget`, `HWWeekly`, `HWTTD`, `HWBudget`, `TotalMach`, `TotalOther`, `TotalOFMach`, ``) VALUES
('1000', 'description text here', '0', '0', '0', NULL, '0', '0', NULL, NULL, '0', NULL, '0', NULL, '0', '0', '0', '0', '0', '0', '0', '0', '0', '20.5', '0', '0', '0', '0', '0', '18.5', '40', '0', '0', '0', '0', '0', '0', '0', '');

表结构:

http://imgur.com/rFLw4c7

现在我意识到该查询中有额外的空字段,但是如何从我的 csv 中删除它们?

以下是我在崇高文本中打开 csv 时的样子:

Job_Num;描述;EngWeekly;EngTTD;EngBudget;DesignWeekly;DesignTTD;DesignBudget;DetailsWeekly;DetailsTTD;DetailsBudget;EjectionWeekly;EjectionTTD;EjectionBudget;CarbonEDMWeekly;CarbonEDMTTD;CarbonEDMBudget;GdrillBMWeekly;GdrillBMTTD;GdrillBMBudget;therWeekly;CNCTTD;QCBCOM MMQCOtherBudget;SpottingWeekly;SpottingTTD;SpottingBudget;SpotPLWeekly;SpotPLTTD;SpotPLBudget;HWWeekly;HWTTD;HWBudget;TotalMach;TotalOther;TotalOFMach 5710;GM K2XL 支架 FRT 5/D 皮带 R;0;0;0;;0;0;;; 0;;0;;0;0;0;0;0;0;0;0;0;20.5;0;0;0;0;0;18.5;40;0;0;0;0;0; 0;0;0;0;457.56;328.8;;;;;

抱歉,我不知道如何更好地构建这个,我将在清除格式之前和之后发布 excel 文件中某一行的图像。

之前:http: //imgur.com/f6jRBZR

之后:http: //imgur.com/M1mQb8V

有没有更有效的方法来做到这一点?

4

2 回答 2

1
INSERT INTO `job_budget_report` 
     ( `Job_Num`
     , `Description`
     , `EngWeekly`
     , `EngTTD`
     , `EngBudget`
     , `DesignWeekly`
     , `DesignTTD`
     , `DesignBudget`
     , `DetailsWeekly`
     , `DetailsTTD`
     , `DetailsBudget`
     , `EjectionWeekly`
     , `EjectionTTD`
     , `EjectionBudget`
     , `CarbonEDMWeekly`
     , `CarbonEDMTTD`
     , `CarbonEDMBudget`
     , `GdrillBMWeekly`
     , `GdrillBMTTD`
     , `GdrillBMBudget`
     , `CNCWeekly`
     , `CNCTTD`
     , `CNCBudget`
     , `MMQCOtherWeekly`
     , `MMQCOtherTTD`
     , `MMQCOtherBudget`
     , `SpottingWeekly`
     , `SpottingTTD`
     , `SpottingBudget`
     , `SpotPLWeekly`
     , `SpotPLTTD`
     , `SpotPLBudget`
     , `HWWeekly`
     , `HWTTD`
     , `HWBudget`
     , `TotalMach`
     , `TotalOther`
     , `TotalOFMach`
     , `` -- clearly, this is the offending line!
     ) VALUES 
     ('1000'
     , 'description text here'
     , '0'
     , '0'
     , '0'
     , NULL
     , '0'
     , '0'
     , NULL
     , NULL
     , '0'
     , NULL
     , '0'
     , NULL
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '20.5'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '18.5'
     , '40'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , '0'
     , ''); -- and its corresponding value
于 2014-02-03T13:12:06.647 回答
1

用于fgetcsv()读取、清理和插入您的表格。- http://www.php.net/fgetcsv

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
     while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
         $num = count($data);
         echo "<p> $num fields in line $row: <br /></p>\n";
         $row++;
         for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
         } 
     }
     fclose($handle);
}
于 2014-02-03T13:15:33.093 回答