如何获得行号时,插入记录从文件到数据库与加载数据本地填充,事务和提交?

ht4b089n  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(319)

在加载数据本地填充、事务和提交的情况下,从文件向数据库插入记录时,如何获取行号?
然而,只有 true 或者 false (失败)与 var 当前转储
声明:

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

脚本:

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

更新1脚本:
试图从中获取行号 EXEC 返回0
脚本:

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

更新2语句:
添加此返回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;
cnjp1d6j

cnjp1d6j1#

解决方法:
完全支持此脚本:

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

但我在一些查询中把它和其他的合并在一起:

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`);

这是错误的,因为 $PDO->exec() 无法返回受影响的表或行数。
相反,要解决我需要一个数组,因为我的脚本支持这样:

$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`);";

相关问题