当使用LOAD DATA LOCAL INFILE,Transaction和Commit将文件中的记录从文件插入到DB中时,如何获取行号?
但是,只有存在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;
}
Update1脚本:
尝试从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;
答案 0 :(得分:0)
解决了:
此脚本已被完全支持:
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`);";