MySQL“ LOAD DATA INFILE”空数据处理

时间:2018-11-03 00:12:10

标签: php mysql pdo

我正在使用以下php脚本,无法弄清为什么它不起作用。我确实得到了它在终端中没有错误处理的回显,但是表仍然为空。代码中是否存在导致此问题的错误信息。我确认它也在目录中找到该文件。希望缺少一些简单的东西。

<?php

try {

$inputfile1 = '////MainDirectory/SubDiretory/data.csv';
$table1 = 'database.testTable1';    
$time1 = microtime(true);

require_once("configFile.php"); //this changes the below as now this is the reference point
    $mysql_host = DB_HOST;
    $mysql_database = DB_NAME;
    $mysql_username = DB_USER;
    $mysql_password = DB_PASS;

    $db = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
    // set the PDO error mode to exception
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

} catch (Exception $e) {
    die("Unable to connect: " . $e->getMessage());
}


try {
    // Return errors
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Begin transaction
    $db->beginTransaction();


if(file_exists($inputfile1)) {
$Found = 'found';
// Query D2 Load Temp table
        $tempA2 = $db->prepare("LOAD DATA INFILE :inputfile1
        INTO TABLE $table1
        FIELDS TERMINATED BY ','
        OPTIONALLY ENCLOSED BY '\"'
        LINES TERMINATED BY '\r\n'
        IGNORE 1 LINES
            (column1, column2, column3);");
        $tempA2->bindParam(':inputfile1', $inputfile1);     
        $tempA2 -> execute();
}


} catch (Exception $e) {
    // If transaction fail, use checkpoint and rollback
    $db->rollBack();
    echo "Conversion failed: " . $e->getMessage().'<br />';
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}   


//time of script    
    //sleep(1);
    $timeC = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
    echo "Full Process Time: {$timeC} $Found";



?>

CSV格式:

"HeaderData1","HeaderData2","HeaderData3","HeaderData4","HeaderData5"
"DataA","DataB","DataC"
"Data2","Data2","Data2"

MySQL表格式

    id        INT  PK with AI and NN
    column1   Varchar
    column2   Varchar
    column3   Varchar

谢谢您的帮助

0 个答案:

没有答案