Page 1 of 1

Import csv file to MySQL with two different fields terminate

Posted: Wed Oct 10, 2012 8:26 am
by krasi_e_d
Hello,

How to import csv file with two different field terminated. I use this code

Code: Select all


$connec = mysql_connect('****','****','****');
mysql_select_db('****', $connec);
$sql = "LOAD DATA LOCAL INFILE '*****.csv'
					INTO TABLE myworksheet 
					FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ';' 
					
					LINES TERMINATED BY '\r\n'
					IGNORE 1 LINES
					";
					
mysql_query($sql);
to import file, but somewhere I have it ";" . How to import this different field.
This is my row from csv file.
[text]
401,1,colomn4,500053;example1;0000000585;21.07.2011,,,,"0,000","0,000","0,000","00,000,000","0,000","0,000","0,000",0,"0,000","0,000","0,000","0,908","0,000","0,000","0,000",0,"0,000","0,000","0,000",0,"0,000","0,000","0,000",0,"0,000","0,000","0,000",908,"0,000","0,000","0,000","00,000,000"
[/text]

Re: Import csv file to MySQL with two different fields termi

Posted: Wed Oct 10, 2012 1:37 pm
by Christopher
You should replace ";" with "," in the text file first -- then import. If you are on Unix the you could exec a command line program like sed from PHP.

Re: Import csv file to MySQL with two different fields termi

Posted: Thu Oct 11, 2012 1:04 am
by krasi_e_d
Christopher wrote:You should replace ";" with "," in the text file first -- then import. If you are on Unix the you could exec a command line program like sed from PHP.
How can I do automatically?

Re: Import csv file to MySQL with two different fields termi

Posted: Thu Oct 11, 2012 5:16 am
by Celauran

Re: Import csv file to MySQL with two different fields termi

Posted: Thu Oct 11, 2012 12:09 pm
by Christopher
You can execute sed from within PHP: http://us1.php.net/manual/en/book.exec.php