dibaliklayar

Friday, June 03, 2005

Another tips for loading data from CSV to MYSQL


This is actulally a dump way to load data, no checking, no parsing, nothing... but hey if you want just to take look at whats in there here you go. if you have column type date, Mysql will not take the date format like this MM/DD/YYYY, it will just put the thing like 0000-00-00. So make everything char, int, or varchar. From sourceforge

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