true)); $PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "open src".PHP_EOL; #打开目标数据库 $PDO_DEST = new PDO($dest_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true)); $PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "open dest".PHP_EOL; #删除目标表中所有数据 fwrite(STDOUT,"deleting date".PHP_EOL) ; try{ $query = "DELETE FROM ".$dest_table; $stmt = $PDO_DEST->prepare($query); $stmt->execute(); }catch(PDOException $e){ fwrite(STDERR,"error:".$e->getMessage()); exit; } fwrite(STDOUT,"deleted date".PHP_EOL) ; // 开始一个事务,关闭自动提交 $count = 0; echo "begin Transaction".PHP_EOL; $PDO_DEST->beginTransaction(); $query = "INSERT INTO ".$dest_table." (id, book , paragraph , word_begin , word_end , length , count , text , html,sim_sents ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? ,?)"; try{ $stmtDEST = $PDO_DEST->prepare($query); }catch(PDOException $e){ fwrite(STDERR,"error:".$e->getMessage()); exit; } #从源数据表中读取 $query = "SELECT * FROM ".$src_table." WHERE true order by id asc"; try{ $stmtSrc = $PDO_SRC->prepare($query); $stmtSrc->execute(); }catch(PDOException $e){ fwrite(STDERR,"error:".$e->getMessage()); exit; } $data = array(); $currMergeCell = 1; while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){ #插入目标表 $currData = array( (int)$srcData["id"], (int)$srcData["book"], (int)$srcData["paragraph"], (int)$srcData["word_begin"], (int)$srcData["word_end"], (int)$srcData["length"], (int)$srcData["count"], $srcData["text"], $srcData["html"], $srcData["sim_sents"]); if($srcData["cell"]==1 ){ $stmtDEST->execute($currData); }else if($srcData["cell"]>1){ $currMergeCell = (int)$srcData["cell"]; $data = $currData; }else{ $data["word_end"] = $srcData["word_end"]; $data["text"] .= " ".$srcData["text"]; $data["html"] .= " ".$srcData["html"]; $data["lenght"] += $srcData["lenght"]; $data["count"] += $srcData["count"]; $currMergeCell--; if($currMergeCell==1){ try{ $stmtDEST->execute($currData); }catch(PDOException $e){ fwrite(STDERR,"error:".$e->getMessage().implode(',',$currData)); exit; } } } $count++; if($count%10000==0){ echo "finished $count".PHP_EOL; } } // 提交更改 $PDO_DEST->commit(); echo "done".PHP_EOL;