我正在尝试使用 PDO 和 LOAD DATA INFILE 将 csv 数据导入 mysql(我也尝试过 LOAD DATA LOCAL INFILE),但我不断收到语法错误,我不知道为什么。文件路径和表名似乎是正确的。
这是我正在使用的 import() 函数 -
function import() {
$this->db = mydb::getConnection();
// set the column names for the selected journal table
if ($this->table = "bsp_journals") {
$columns = "category, discipline, subject, sourcetype, issn, publicationname, indabstart, indabstop, fulltextstart, fulltextstop, fulltextdelay, peerreviewed";
}
try {
$sql = "LOAD DATA LOCAL INFILE '$this->file'
INTO TABLE '$this->table'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\n'
OPTIONALLY ENCLOSED BY '\"'
($columns)";
$statement = $this->db->prepare($sql);
$statement->execute();
$this->return = $this->files['filename']['tmp_name'];
} catch (PDOException $ex) {
//throw $ex;
$this->return = $ex->getMessage() . "<br /></br />" . $sql . "<br /></br />File Name = " . $this->file;
}
return $this->return;
}
下面是我收到的消息,包括 mysql 错误消息和 SQL 查询
- import result : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''bsp_journals' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ' at line 2
LOAD DATA LOCAL INFILE 'files/buh-journals.csv' INTO TABLE 'bsp_journals' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' OPTIONALLY ENCLOSED BY '"' (category, discipline, subject, sourcetype, issn, publicationname, indabstart, indabstop, fulltextstart, fulltextstop, fulltextdelay, peerreviewed)
File Name = files/buh-journals.csv