【发布时间】:2014-02-17 13:57:45
【问题描述】:
我需要将 100 多个 CSV 文件加载到 MySQL 中,因此我正在编写一个脚本来执行此操作。除其他外,我有以下 sn-p,它导入每个 CSV 文件:
private function importFile(){
if($this->connection->beginTransaction()){
$transactionFailed = false;
$importStatement = $this->connection->prepare("
LOAD DATA INFILE :file
REPLACE INTO TABLE :table;
");
foreach($this->fetchFileList() as $file){
$executeSuccesfull = $importStatement->execute(array(
':file' => $this->path . DIRECTORY_SEPARATOR . $file,
':table' => $file
));
if(!$executeSuccesfull){
$transactionFailed = true;
$this->fetchDBError($importStatement);
$this->connection->rollBack();
break;
}
}
if(!$transactionFailed){
$this->connection->commit();
}
}
else{
$this->fetchDBError();
}
}
$this->path 指向所有 CSV 文件所在的路径(我检查过)。每个$file 与应导入其数据的表的名称完全相同,没有文件扩展名(其中任何一个都没有.csv)。我正在使用在 Windows XP 之上运行的 PHP 5.3.9、MySQL 5.0.27。所有这些都在我的本地机器上运行。
问题是,我收到以下错误:
[ERROR 42000] — 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
''the_first_file'' at line 2 (1064)
The query issued was:
LOAD DATA INFILE :file
REPLACE INTO TABLE :table;
这段代码是由我班级的另一个方法生成的:
private function fetchDBError($statement = null){
$errorCode = null;
$errorInfo = null;
$queryString = '';
if($statement){
$errorInfo = $statement->errorInfo();
$errorCode = $statement->errorCode();
$queryString = "<p>The query issued was:</p><pre>{$statement->queryString}</pre>";
}
else{
$errorInfo = $this->connection->errorInfo();
$errorCode = $this->connection->errorCode();
}
$this->success = false;
$this->message = "<p>[ERROR $errorCode] {$errorInfor[0]} — {$errorInfo[2]} ({$errorInfo[1]})</p>$queryString";
【问题讨论】: