2014-02-17 2 views
0

100 개 이상의 CSV 파일을 MySQL에로드해야하므로이 작업을 수행하는 스크립트를 작성하고 있습니다. 모든 CSV 파일이있는 경로로PDO를 사용하여 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). 나는 PHP 5.3.9를 사용 중이며, MySQL 5.0.27은 Windows XP상에서 실행 중이다. 그것들 모두는 내 로컬 컴퓨터에서 실행됩니다. 이 코드는 내 클래스의 다른 방법에 의해 생성 된

[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; 

:

문제가

, 나는 다음과 같은 오류에 geting있어

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"; 

답변

0

하나 이상의 파일이 aphostophe을 포함을 시도

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

그런 다음 쿼리 매개 변수에 $ filepath를 사용하십시오.

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

하기로 종료 필드를 지정하려고 :

은 MySQL의 데이터를로드 기능의 예입니다.

stackoverflow 게시물에서 나는 준비 함수 및 LOAD DATA에 대해 this을 찾았습니다.

+0

여전히 똑같습니다. 첫 번째 파일에는 아포스트로피가 없으며 파일 이름에도 아포스트로피가 없습니다. – Metalcoder

+0

변경 사항 없음. 내 CSV 파일 형식과 일치하는'FIELDS TERMINATED BY '\ t''를 넣으려고했으나 아무런 개선이 없었습니다. – Metalcoder

+0

매개 변수 뒤에 작은 따옴표를 추가하십시오. file (': file') – Ganz

관련 문제