How to get Row Number when insert records from File to DB with LOAD DATA LOCAL INFILE, Transaction and Commit?

非 Y 不嫁゛ 提交于 2019-12-11 17:08:28

问题


How to get Row Number when insert records from File to DB with LOAD DATA LOCAL INFILE, Transaction and Commit?

However, only getting true or false(in fail) with the var dump present

Statement:

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)

Script:

public function PDO_UL_IUPD($dbUsing, $stmtpre) {
        $started = microtime(true);
        $DB      = [];
        $val     = [];
        $conn    = new PDO(
            "mysql:host=" . DB_HOST . ";dbname=" . DB_PRE . "" . $dbUsing . "",
            DB_USERNAME,
            DB_PASS,
            array(
                PDO::MYSQL_ATTR_LOCAL_INFILE       => TRUE,
                PDO::MYSQL_ATTR_INIT_COMMAND       => "SET NAMES UTF8",
                PDO::ATTR_EMULATE_PREPARES         => FALSE,
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
                PDO::ATTR_ERRMODE                  => PDO::ERRMODE_EXCEPTION,
            ));
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        try {
            $conn->beginTransaction();
            $conn->exec("set names utf8");
            $count  = 0;
            $RowReg = 0;
            // our SQL statements
            foreach ($stmtpre as $stmt) {
                $count++;
                $conn->exec($stmt);
            }
            $RowReg = $conn->commit();
            echo var_dump($RowReg);
            $DB['SMG'] = "Correct Execution, a csv file has been dumped.<br><br>";
            $DB['R']   = true;
        } catch (PDOException $e) {
            $DB['SMG'] = "Error: " . $e->getMessage();
            $DB['R']   = false;
        }
        return $DB;
    }

Update1 Script:

Trying to get Row Number from EXEC return 0

Script:

public function PDO_UL_IUPD($dbUsing, $stmtpre) {
        $started = microtime(true);
        $DB      = [];
        $val     = [];
        $conn    = new PDO(
            "mysql:host=" . DB_HOST . ";dbname=" . DB_PRE . "" . $dbUsing . "",
            DB_USERNAME,
            DB_PASS,
            array(
                PDO::MYSQL_ATTR_LOCAL_INFILE       => TRUE,
                PDO::MYSQL_ATTR_INIT_COMMAND       => "SET NAMES UTF8",
                PDO::ATTR_EMULATE_PREPARES         => FALSE,
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE,
                PDO::ATTR_ERRMODE                  => PDO::ERRMODE_EXCEPTION,
            ));
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        try {
            $conn->exec("set names utf8");
            $count  = 0;
            $RowReg=0;
            $cReg=0;
            // our SQL statements
            foreach ($stmtpre as $stmt) {
                //echo $stmt."<br><br><br>";
                $count++;
                $cReg=$conn->exec($stmt);
                $RowReg=$RowReg+$cReg;
            }
            $conn->commit();
            echo var_dump($RowReg);
            $DB['SMG'] = "Correct Execution, a csv file has been dumped.<br><br><br>";
            $DB['R']   = true;
        } catch (PDOException $e) {
            $DB['SMG'] = "Error en Tiempo de Ejecucion: " . $e->getMessage();
            $DB['R']   = false;
        }
        return $DB;
    }

Update 2 statement:

adding this Return 1

SET @row=0;
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)
SET file_line_no = @row:=@row+1;

回答1:


Solved with:

This script is full suported:

LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)

But i merge it with other in some Query Like that:

DELETE FROM tablename WHERE date BETWEEN '$Date1' AND '$Date2';
ALTER TABLE tbalename AUTO_INCREMENT = 1;
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
    FIELDS TERMINATED BY ',' 
    LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`);

this is wrong, becouse $PDO->exec() Cant return the number of affected table or rows.

Instead of it, to solve i need past an array, becouse my script support it like that:

$stmtpre[1] = "DELETE FROM tablename WHERE date BETWEEN '$Date1' AND '$Date2';";
$stmtpre[2] = "ALTER TABLE tbalename AUTO_INCREMENT = 1;";
$stmtpre[3] = "LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`);";


来源:https://stackoverflow.com/questions/52337140/how-to-get-row-number-when-insert-records-from-file-to-db-with-load-data-local-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!