How can we import the text file, having data on the same line with a separator, into MySQL table?

MySQLMySQLi Database

Actually, we can write the data on the same line in the text file by using a separator. In this case, while importing this text file into MySQL table then we must have to use ‘LINES TERMINATED BY’ option. It can be understood with the help of the following example −

Suppose we are using ‘|’ as the LINE TERMINATOR symbol in a text file as follows −

id,Name,Country,Salary|105,Chum*,Marsh,USA,11000|106,Danny*,Harrison,AUS,12000

Now while importing this text file into MySQL table then we need to mention ‘LINE TERMINATED BY’ option also in the query as follows −

mysql> LOAD DATA LOCAL INFILE 'd:\A.txt' INTO table employee7_tbl FIELDS TERMINATED BY ',' ESCAPED BY '*' LINES TERMINATED BY '|'IGNORE 1 ROWS;
Query OK, 2 rows affected (0.05 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0

Now, we can see what has been imported with the help of the following query −

mysql> Select * from employee7_tbl;
+------+----------------+----------+--------+
| Id   | Name           | Country  | Salary |
+------+----------------+----------+--------+
| 105  | Chum,Marsh     | USA      |  11000 |
| 106  | Danny,Harrison | AUS      |  12000 |
+------+----------------+----------+--------+
2 rows in set (0.00 sec)
Updated on 04-Feb-2020 06:08:24