我正在下载并将远程文本文件导入到本地mysql数据库。
格式最近已更改为在其中一列中包含逗号,导致导入失败,并显示错误“第1行的CSV输入中的字段数无效”。数据现在看起来像:
"bonita","FL","212025920","2012929","Hooker,Bill"
如何更新此代码段以允许引号内的逗号?
foreach(explode($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
$linemysql = str_replace("\"","",$linemysql);###code added
$query = "insert ignore into $databasetable values('$linemysql');";
答案 0 :(得分:3)
LOAD DATA LOCAL INFILE
选项的 FIELDS TERMINATED BY ',' ENCLOSED BY '"'
应直接读取该文件。 LOAD DATA INFILE
也比标准INSERT
语句更快。
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
这很接近,但我认为TRIM()不会删除标签:
LOAD DATA LOCAL INFILE '/my/local/file/path.csv' IGNORE INTO TABLE mytable FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (@a, @b, @c, @d, @e) SET col1=TRIM(@a), col2=TRIM(@b), col3=TRIM(@c), col4=TRIM(@d) col5=TRIM(@e)
答案 1 :(得分:2)
将您的功能重写为:
foreach(explode($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
$lineArray = str_getcsv($line, ",", '"');
$linemysql = implode("','",$lineArray);
$query = "insert ignore into $databasetable values('$linemysql');";
//.. Handle the rest
}
php getcsv函数负责分隔符并跟踪附件 - 为您节省这些麻烦。
以下代码段:
$line = '"bonita","FL","212025920","2012929","Hooker,Bill"';
$array = str_getcsv($line, ",", '"');
print_r($array);
输出:
Array ( [0] => bonita [1] => FL [2] => 212025920 [3] => 2012929 [4] => Hooker,Bill )
答案 2 :(得分:0)
样本表:
CREATE TABLE `table1` (
`id` VARCHAR(50) NULL DEFAULT NULL,
`a1` VARCHAR(50) NULL DEFAULT NULL,
`b1` VARCHAR(50) NULL DEFAULT NULL,
`c1` VARCHAR(50) NULL DEFAULT NULL,
`d1` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoD1B;
导入查询
load data local infile 'd:/import.csv' into table table1 fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
(id, a1, b1, c1, d1)
<强> import.csv 强>
"bonita1","FL1","212025920","2012929","Hooker,Bill"
"bonita2","FL2","212025920","2012929","Hooker,Bill"
"bonita3","FL3","212025920","2012929","Hooker,Bill"