【问题标题】:Load a CSV file using PDO results in a syntax error使用 PDO 加载 CSV 文件会导致语法错误
【发布时间】: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]} &mdash; {$errorInfo[2]} ({$errorInfo[1]})</p>$queryString";

【问题讨论】:

    标签: php mysql csv pdo


    【解决方案1】:

    一个或多个文件包含aphostophe,尝试使用这个:

          $filepath = $this->path . DIRECTORY_SEPARATOR .  str_replace("'", "''", $file);
    

    然后在查询参数中使用$filepath。

    这是mysql加载数据函数的一个例子:

    LOAD DATA INFILE '$filepath' INTO TABLE test
    FIELDS TERMINATED BY ',';
    

    同时尝试指定 FIELDS TERMINATED BY。

    在一个 stackoverflow 帖子中,我找到了 this 关于准备函数和加载数据的信息。

    【讨论】:

    • 还是一样。第一个文件没有撇号,也没有文件名。
    • 仍然没有变化。我尝试输入与我的 CSV 文件格式匹配的 FIELDS TERMINATED BY '\t',但没有任何改进。
    • 尝试在参数:file(':file')前后加单引号
    • PDO 会填充准备好的语句中的所有单引号,因此当您尝试手动插入它们时会呈现错误。反正我试过了:Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
    • 尝试阅读这篇文章:stackoverflow.com/questions/17533151/…
    猜你喜欢
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多