toggle Cinema 04 Mar 2009

Parse your file for save into your Mysql-table ? Think again Posted by Insane in Uncategorized | No responses

Sometimes you will need to grab the content of a file and place it into your table , depending of whether your file share a delimited character structure you can easily avoid using php for make the script that parse-save your data.

Mysql comes with a pre-build command (LOAD DATA INFILE) for make your life simpler and easier , for instance let´s say that you have a csv file (that as you know it´s a simple row file coma-separated-values format) with name and email placed each one in a column, and your mysql table looks similar to:

desc tb001;
| name     | char(10) | YES  |     | NULL    |       |
| email    | char(25) | YES  |     | NULL    |       |

Well, you could load your file by using the command of above.

LOAD DATA INFILE '/data.csv' INTO TABLE db.tb001 FIELDS TERMINATED BY ',' ENCLOSED BY '""' ESCAPED BY '\\' LINES TERMINATED BY '\n';

What you´re saying is load the file /data.csv ( if is not an absolute file systempath won’t work) using the options that follows after ‘db.t001′.

One line fans:

mysql -uuser -ppassword db -e "LOAD DATA INFILE '/data.csv' INTO TABLE db.tb001 FIELDS TERMINATED BY ',' ENCLOSED BY '\"\"' ESCAPED BY '\\' LINES TERMINATED BY '\n'"

Hope this results usefull to someone.

  • Share/Bookmark

Tags: , , ,

Promote this post

Would you like to add this post to your bookmarks? Come on, do not miss any updates and stay tuned.
Please share with us!

This entry no have comments but you can be first .

Leave a Reply ( Guest )

(*)

(will not be published) (*)