示例以下是 a.txt 文件中的逗号分隔值 -
id,name,country,salary100,”ram”,”india”,25000101,”mohan”,”india”,28000
我们希望将此数据导入到以下名为employee3_tbl的文件中 -
mysql> create table employee3_tbl(id int, name varchar(20), country varchar(20),salary int);query ok, 0 rows affected (0.1 sec)
现在,可以借助下表将数据从文件传输到数据库表 -
mysql> load data local infile 'd:\a.txt' into table employee3_tbl fields terminated by ',' enclosed by ‘“’ ignore 1 rows;query ok, 2 rows affected (0.16 sec)records: 2 deleted: 0 skipped: 0 warnings: 0
在上面的查询中,mysql 将忽略第一行。忽略行取决于“ignore n rows”选项中“n”位置给出的值。
mysql> select * from employee3_tbl;+------+-------+---------+--------+| id | name | country | salary |+------+-------+---------+--------+| 100 | ram | india | 25000 || 101 | mohan | india | 28000 |+------+-------+---------+--------+2 rows in set (0.00 sec)
上面的结果集显示a.txt文件中的数据已经传输到表中。
以上就是我们如何从第一行包含列名称的文本文件导入数据?的详细内容。