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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵