douchun5969 2013-03-21 06:02
浏览 163
已采纳

逗号在地址时将CSV文件导入MySQL的问题

I'm using a script that I found online to import CSV file into MySQL: http://www.johnboy.com/blog/tutorial-import-a-csv-file-using-php-and-mysql but when the script detects a comma in an address like this My address, CO 80113 in a cell, it also splits there as well.

I've seen a solution where you can save from excel to a Tab Delimited txt file. Then you go into notepad and replace the tabs with semicolons.

Is this the best practice for "fixing the comma in the address" issue?

My end goal is to take a CSV file from Highrise full of hundreds of clients into our MySQL database, then make updates every couple of months so this seems to be a decent script but am I going at this the wrong way?

EDIT : It appears that the part of the PHP code that splits up the cells is this

while ($data = fgetcsv($handle,1000,",","'")); 
  • 写回答

2条回答 默认 最新

  • duanmi4379 2013-03-21 07:16
    关注

    If you use comma as column separator, then you should quote all string field values, e.g. -

    1,'My address, CO 80113'
    2,'His address, CO 80114'
    4,'Her address, CO 80115'
    and so on
    

    Try to use LOAD DATA INFILE statement with FIELDS ENCLOSED BY '\'' option.

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

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办