源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

mysql中Load Data记录换行问题的解决方法

  • 时间:2020-08-27 13:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:mysql中Load Data记录换行问题的解决方法
问题是这样的: 表persons有两个字段: id和name 文本文档persons.txt中内容(其中每行字段之间用tab分割): 1    Bush 2    Carter 3    Bush 在mysql命令行下使用 load data local infile “persons.txt” into table persons 导入数据到persons表中。 导入后查看persons表的数据,与persons.txt的内容一致。但是使用语句 select distinct name from persons 查询,结果中Bush出现了两次(正常结果应该是Bush只出现一次)。 [b]原因分析: [/b]经过分析,发现原因是windows下换行符为"\r\n",而mysql在load data时默认使用"\n"来切割每行记录,导致插入到表中前两条记录的name字段末尾多插入了不可见字符"\r";也就是说使用distinct关键字查询出来的两个Bush中,第一个词尾有回车符"\r”,而第二个词尾没有。 说明: 1. mysql默认使用tab来分割每行的字段。 2. 因为linux下换行符为"\n",所以在linux下不会出现上述问题。 [b]修改方法: [/b]只要在导入数据时指定以"\r\n"来换行就可以了。 修改后的导入数据语句为:
[u]复制代码[/u] 代码如下:
load data local infile “persons.txt” into table persons lines terminated by “\r\n”;
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部