Suppose you have a data textfile named textfilename.txt that contains 5 comma separated columns of data on each line and each columns enclosed by ‘”‘ (double quotes). You want to import this textfile into your MySQL table named mytablename, which has 5 columns that are named field1, field2, field3, field4 and field5.
To import the datafile, type the following SQL at the mysql prompt:
LOAD DATA LOCAL INFILE ‘textfilename.txt’
INTO TABLE mytablename
FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\r\n’
(field1, filed2, field3, field4, field5);
The above SQL statement tells the MySQL server to find textfilename.txt on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, each columns enclosed by double quotes and to put it into your MySQL mytablename as columns field1, field2, field3, field4, and field5 respectively.
Be First To Comment
Related Post
Leave Your Comments Below