Import Unicode CSV Files T 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 authors;

or

mysql> DELETE FROM authors;

The import will be executed as follows:

mysql> LOAD DATA LOCAL INFILE ‘authors.txt’ INTO TABLE authors FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\r\n’;

for a CSV file ‘authors.txt’ with records having data fields as follows:

“authid”,”lastname”,”firstname”,”address”,”city”,”country”,”phone”,”email”
1,”Nguyễn”,”Trần”,”Lý Thường Kiệt”,”Sài Gòn”,”Việt Nam”,”848-999-9999″,”nguyentran@yahoo.com”
2,”Lê”,”Lý”,”Phố Quang Trung”,”Hà Nội”,”Việt Nam”,”848-888-8888″,”lely@yahoo.com”

The line terminator ‘\r\n’ is for Windows systems; for Unix/Linux, ‘\n’ is used.

Advertisements

4 thoughts on “Import Unicode CSV Files T MySQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s