mysql导入数据(load data local infile)

/*
1.测试环境是windows,filepath需要用'\\',cmd中filepath可以有中文,navicat不支持中文

2.mysql默认EOL是'\n',windows需要指定为'\r\n',否则最后一列数据都会带'\r',db中看不出来,where clause匹配不到

3.字段分隔符:fields terminated必须在lines terminated之前,否则报错

4.ignore 1 lines不导入标题

5.速度:71万条记录,13个字段,一次37s,一次26s
*/
--windows下的正确写法
load data local infile 'G:\\temp\\我的数据.csv' into table table_name fields terminated by ',' lines terminated by '\r\n' ignore 1 lines;

--'\r'的处理
insert into table(col1,col2) values(1,'a\r');       --col2长度为2
update table col2=replace(col2,'\r','') where col1 = 1;     --替换掉'\r'
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
load data local infileMySQL中用于将文本文件数据导入数据库表中的命令。它是用来导入单张表的,无法同时导入多张表。 要导入多张表,我们可以将每张表的数据保存为单独的文本文件,然后分别使用load data local infile 命令导入每个文件到相应的表中。 例如,假设我们有两个表table1和table2,分别保存在文件table1.txt和table2.txt中。我们可以依次执行以下load data local infile命令将数据导入到对应的表中: load data local infile 'table1.txt' into table table1 fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' (column1, column2, column3); load data local infile 'table2.txt' into table table2 fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' (column4, column5, column6); 其中,'table1.txt'是table1表的数据文件路径,table1是目标表名,fields terminated by ',' optionally enclosed by '"'是确定字段分隔符和文本包装符的设置,lines terminated by '\n'指定行分隔符,(column1, column2, column3)是对应列名。 可以根据需要在导入命令中适当调整字段分隔符、文本包装符和行分隔符等参数,以适应不同数据文件的格式。同样的原理也适用于导入更多表的数据。 总的来说,通过将每个表的数据保存为不同的文本文件,并使用适当的load data local infile命令逐一导入,我们可以实现同时导入多张表的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值