How to insert csv file data into mysql database

前端 未结 2 742
情书的邮戳
情书的邮戳 2021-01-24 21:29

I want to insert data into mysql table from csv file. Import data from region_codes.csv file. In region_codes.csv file having 3 columns in 3rd columns it had , sepa

相关标签:
2条回答
  • 2021-01-24 22:02

    You can use mysqlimport tool this way:

    mysqlimport  --ignore-lines=1 --fields-terminated-by=,
    --columns='ID,Name,Phone,Address' --local -u root -p
    Database /path/to/csvfile/TableName.csv 
    

    Here is a full explanation: http://chriseiffel.com/everything-linux/how-to-import-a-large-csv-file-to-mysql/

    0 讨论(0)
  • 2021-01-24 22:06

    You can try below syntax if it works for you otherwise provide csv data:

    LOAD DATA LOCAL INFILE 'C:/region_codes.csv' INTO TABLE `region_codes` FIELDS ESCAPED BY '\\' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
    

    If above syntax does not work then export data by below command again and import by below given command.

    select * into outfile 'C:/region_codes.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from `region_codes`;
    

    Now use below command (to ignore column heading line)

    LOAD DATA LOCAL INFILE 'C:/region_codes.csv' INTO TABLE `region_codes` FIELDS ESCAPED BY '\\' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
    

    Note: If data is prepared manually then need to correct it manually.

    If still not work then attach your csv data to check exact problem.

    0 讨论(0)
提交回复
热议问题