我有一个像这样格式的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”等数字。
请帮助我!
答案 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