dongxuanchao1425 2014-09-29 11:51
浏览 23
已采纳

PHP,MySQL - 插入带有标题的CSV文件

I have CSV file with header and data fields. How can I insert this via SQL query and remove headers. At the moment code that I wrote is working, but it is inserting header as a data into DB.

The code:

   $files  = directory_map('./assets/csv/');
    foreach ($files as $file) :
        $filefile = './assets/csv/' . $file;
        $q = "LOAD DATA LOCAL INFILE '$filefile' INTO TABLE person FIELDS TERMINATED BY ','  
              LINES TERMINATED BY '
'  (personal_name, personal_lastname, 
              personal_country,personal_address,contact_email,dateadded);";
        $this->db->query($q);           
    endforeach;
  • 写回答

2条回答 默认 最新

  • doulue1949 2014-09-29 11:54
    关注

    You can use the ignore xx lines clause in load data (the doc on this)[http://dev.mysql.com/doc/refman/5.1/en/load-data.html] skip down about a third of the way:

        $q = "LOAD DATA LOCAL INFILE '$filefile' INTO TABLE person FIELDS TERMINATED BY ','  
              LINES TERMINATED BY '
    ' ignore 1 lines (personal_name, personal_lastname, 
              personal_country,personal_address,contact_email,dateadded);";
    

    This will cause the input to skip 1 line of the CSV - which I am assuming is your headers. Skip however many lines are needed to get to the data itself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值