mySQL加载数据本地infile数字格式不正确

时间:2011-10-26 09:57:36

标签: mysql

我有一个像这样格式的csv文件,

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';

然后截断“100,000,000”,“500,000”等数字。

请帮助我!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在php中尝试这种方法:

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

参考:fgetcsv

相关问题