如何将包含一些行前缀的文本文件导入 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)
广告