Another tips for loading data from CSV to MYSQL
Import Unicode CSV files to MySQL
Unicode UTF-8 comma-separated values (CSV) text files, which are exported or generated by such applications as Microsoft Access or Excel, can be imported to MySQL via LOAD DATA INFILE command. CSV data files that are in a Vietnamese legacy encoding should first be converted to Unicode UTF-8, using UnicodeConverter tool, before proceeding with the import.
Make sure that MySQL default charset is utf8. You may need to create the schema (i.e., database structure and tables) before executing the LOAD DATA command. This can be accomplished manually or by MySQL Migration Toolkit to re-create the schema in MySQL database and then use TRUNCATE command to clear the table (delete all rows) before importing. For example:
mysql> TRUNCATE TABLE Ngarang; (better in my opinion, it will reset the auto increment field)
or
mysql> DELETE FROM Ngarang;
The import will be executed as follows:
mysql> LOAD DATA LOCAL INFILE 'Ngarang.txt' INTO TABLE Ngarang FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
for a CSV file 'Ngarang.txt' with records having data fields as follows:
"authid","lastname","firstname","address","city","country","phone","email"
1,"Bejo","Kandi","Kraton Jawa","Jawa","Indonesia","234-999-9999","Sri_kandi@yahoo.com"
2,"I made","Nyoman","Keraton Bali","Denpasar","Indonesia","234-888-8888","Bejo_jone@yahoo.com"
The line terminator '\r\n' is for Windows systems; for Unix/Linux, '\n' is used. If you don't put `LINES TERMINATED BY` it will take new lines char by default.
0 Comments:
Post a Comment
<< Home