1.首先,必须创建一个一一对应的表(字段对应的数据的类型一一对应)。
假如创建一个用户表user:
create table user(
id int unsigned not null primary key auto _incremarent,
username varchar(50) not null,
password varchar(50) not null
)
2.然后,将文件导入到数据库:
假设文件名为user.txt,内容为:
1,test001,test001
2,test002,test002
3,test003,test003
4,test004,test004
执行导入命令(假设文件在c盘根目录):
mysql>load data infile 'c:/user.txt' into table user
>fields terminated by','
>lines terminated by'\r\n'
注意:
1),文件可以使用绝对路径c:/user.txt,否则需放在数据库根目录中
2),因为字段数据之间是用逗号隔开的,所以必i须使用fields terminated by','不然将导入失败。
3),因为Winsows中行以“\r\n”隔开,所以必须lines terminated by'\r\n',

本文介绍了一种将文本文件中的数据批量导入到MySQL数据库的方法。具体步骤包括:首先创建一个与文本文件字段类型相对应的数据库表,然后通过LOAD DATA INFILE命令将文件中的数据导入到指定的表中。文章特别强调了配置字段分隔符和行终止符的重要性。
8174

被折叠的 条评论
为什么被折叠?



