如何将包含一些行前缀的文本文件导入到 MySQL 表中?
假设如果我们的文本文件中有行前缀,那么借助于使用“LINES STARTING BY”选项,我们可以忽略该前缀并将正确的数据导入到 MySQL 表中。可以通过以下示例来理解:
示例
假设我们在文本文件中使用“VALUE”作为“行前缀”,如下所示:
id, Name, Country, Salary VALUE:105, Chum*, Marsh,USA, 11000 106, Danny*, Harrison,AUS, 12000
现在,在将此文本文件导入到 MySQL 表中时,我们需要在查询中也提及“LINES STARTING BY”选项,如下所示:
mysql> LOAD DATA LOCAL INFILE 'd:\A.txt' INTO table employee8_tbl FIELDS TERMINATED BY ',' ESCAPED BY '*' LINES STARTING BY 'Value:' IGNORE 1 ROWS; Query OK, 1 row affected (0.07 sec) Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
现在,我们可以通过以下查询查看已导入的内容:
mysql> LOAD DATA LOCAL INFILE 'd:\A.txt' INTO table employee8_tbl FIELDS TERMINATED BY ',' ESCAPED BY '*' LINES STARTING BY 'Value:' IGNORE 1 ROWS; Query OK, 1 row affected (0.07 sec) Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
现在,我们可以通过以下查询查看已导入的内容:
mysql> Select * from employee8_tbl; +------+----------------+----------+--------+ | Id | Name | Country | Salary | +------+----------------+----------+--------+ | 105 | Chum,Marsh | USA | 11000 | +------+----------------+----------+--------+ 1 row in set (0.00 sec)
MySQL 在表中只导入了一条记录,因为我们在第一行之前使用了行前缀“VALUE:”,因此它忽略了没有行前缀的行。现在,假设我们在文本文件中添加另一行,并带有行前缀“VALUE:”,那么 MySQL 也会上传此行:
id, Name, Country, Salary Value:105, Chum*, Marsh,USA, 11000 106, Danny*,Harrison, AUS, 12000 Value:107, Raman*,Kumar, IND, 25000
mysql> LOAD DATA LOCAL INFILE 'd:\A.txt' INTO table employee8_tbl FIELDS TERMINATED BY ',' ESCAPED BY '*' LINES STARTING BY 'Value:' IGNORE 1 ROWS; Query OK, 2 rows affected (0.07 sec) Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
现在,我们可以通过以下查询查看已导入的内容:
mysql> Select * from employee8_tbl; +------+----------------+----------+--------+ | Id | Name | Country | Salary | +------+----------------+----------+--------+ | 105 | Chum,Marsh | USA | 11000 | | 107 | Raman,Kumar | IND | 25000 | +------+----------------+----------+--------+ 2 rows in set (0.00 sec)
广告