2011-10-26 23 views
0

我有格式化這樣一個csv文件,mySQL的負載數據本地infile的數目不正確格式

sth, sth, "100,000,000", sth, "200,000" 
sth, sth, "200,000,000", sth, "500,000" 

當我使用

mysql> load data local infile "thefile.csv" into table mytable terminated by ',' enclosed by '"' lines terminater by '\r\n'; 

則號碼如「億」,「500000」是截斷。

請幫幫我!

+0

截斷如何?你在加載數據的領域是什麼樣的領域? –

回答

0

試試這個辦法在PHP中:

$in = fopen('filename.csv', 'r'); 
while (($row = fgetcsv($in, 10000, ',', '"')) !== false) { 
    mysql_query("INSERT INTO ...."); 
} 
fclose($in); 

參考:fgetcsv

+0

我不確定這是否會比OP的原始代碼更好 - 看到問題可能是數字中的逗號 –

+0

我認爲它可行,這要歸功於'$ enclosure ='「'' – JellyBelly

3

從CSV文件刪除空格:

sth,sth,"100,000,000",sth,"200,000" 
sth,sth,"200,000,000",sth,"500,000" 

,並嘗試使用該語句加載數據 -

LOAD DATA INFILE 'thefile.csv' INTO TABLE mytable 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' 
(column1, column2, @var1, column4, @var2) -- specify actual field names 
SET column3 = REPLACE(@var1, ',', ''), column5 = REPLACE(@var2, ',', ''); -- remove thousand separators 
+0

非常感謝我爲我工作。 – phucle