从CSV跳过行加载数据

时间:2015-11-13 10:22:59

标签: php mysql csv

我创建了一个简单的查询来将.csv文件加载到我的MySQL数据库,它看起来像:

    $sql = "LOAD DATA INFILE '".$filePath."' 
            INTO TABLE r_vehicle_submodel 
            CHARACTER SET UTF8
            FIELDS TERMINATED BY ',' 
            ENCLOSED BY '\"' 
            IGNORE 1 LINES ";  

听起来很一般,没什么特别的。我扔了一个带有29 000行的.csv,sctructure如下:

07,"BENELLI","81","90"
08,"BETA","81","90"
09,"BMW","81","90"
10,"BUFFALO","81","90"
11,"CAGIVA","81","90"
12,"CCF","81","90"
13,"CPI","81","90"

使用表结构:

code(varchar(2)) UTF8
name(varchar(40)) UTF8
model_code(varchar(2)) UTF8
manufacturer_code(varchar(2)) UTF8

但是从29k线开始,只有14k被实际导入到桌面。在导入期间没有发生错误,我看到数据中没有任何损坏,导入时仍会跳过某些行。

你能告诉我哪里有问题或我应该检查什么来调试这个?

P.S。 我的完整代码如下:

    try{
            $fileName =  $this->upload();               
            $rootDirectory = dirname(__FILE__);             
            $filePath = $rootDirectory.$fileName;                                                                                                                      
            $con = new PDO( LOCAL_DB_HOST, LOCAL_DB_USER, LOCAL_DB_PASS );  
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $sql = "LOAD DATA INFILE '".$filePath."' 
                    INTO TABLE r_vehicle_submodel 
                    CHARACTER SET UTF8
                    FIELDS TERMINATED BY ',' 
                    ENCLOSED BY '\"' 
                    IGNORE 1 LINES ";      

            $stmt = $con->prepare( $sql );
            $stmt->execute();
            echo "Data uploaded to MySQL <br>";
      }catch(PDOExeption $e){
                echo $e->getMessage();
                echo $con->errorInfo();
      }    

编辑:

  1. 从那29k行开始,它会随机选择它插入的内容,哪些不是,它不像第一个14k。
  2. 脚本没有超时,所有实际上最多需要5秒钟。)

0 个答案:

没有答案