db_insert_bookword_from_csv.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <h2>Insert to Index</h2>
  7. <?php
  8. include "./_pdo.php";
  9. include "../path.php";
  10. if(isset($_GET["from"])==false){
  11. ?>
  12. <form action="db_insert_bookword_from_csv.php" method="get">
  13. From: <input type="text" name="from" value="0"><br>
  14. To: <input type="text" name="to" value="216"><br>
  15. <input type="submit">
  16. </form>
  17. <?php
  18. return;
  19. }
  20. $from=$_GET["from"];
  21. $to=$_GET["to"];
  22. $dirLog=_DIR_LOG_."/";
  23. $dirXmlBase=_DIR_PALI_CSV_."/";
  24. $filelist=array();
  25. $fileNums=0;
  26. $log="";
  27. echo "<h2>doing : No.{$from} book </h2>";
  28. global $dbh_word_index;
  29. $dns = "sqlite:"._FILE_DB_BOOK_WORD_;
  30. $dbh_word_index = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  31. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  32. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  33. while(($filelist[$fileNums]=fgetcsv($handle,0,','))!==FALSE){
  34. $fileNums++;
  35. }
  36. }
  37. if($to==0 || $to>=$fileNums) $to=$fileNums-1;
  38. $bookword = array();
  39. if(($fpoutput=fopen(_DIR_CSV_PALI_CANON_WORD_."/{$from}_words.csv", "r"))!==FALSE){
  40. $count=0;
  41. while(($data=fgetcsv($fpoutput,0,','))!==FALSE){
  42. $book = $data[1];
  43. if(isset($bookword[$data[3]])){
  44. $bookword[$data[3]]++;
  45. }
  46. else{
  47. $bookword[$data[3]] = 1;
  48. }
  49. $count++;
  50. }
  51. }
  52. // 开始一个事务,关闭自动提交
  53. $dbh_word_index->beginTransaction();
  54. $query="INSERT INTO bookword ('book','wordindex','count') VALUES ( ? , ? , ? )";
  55. $stmt = $dbh_word_index->prepare($query);
  56. foreach ($bookword as $key => $value) {
  57. $stmt->execute(array($book,$key,$value));
  58. }
  59. // 提交更改
  60. $dbh_word_index->commit();
  61. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  62. $error = $dbh_word_index->errorInfo();
  63. echo "error - $error[2] <br>";
  64. $log.="$from, $FileName, error, $error[2] \r\n";
  65. }
  66. else{
  67. echo "updata $count recorders.<br />";
  68. $log.="updata $count recorders.\r\n";
  69. }
  70. $myLogFile = fopen($dirLog."insert_index.log", "a");
  71. fwrite($myLogFile, $log);
  72. fclose($myLogFile);
  73. if($from>=$to){
  74. echo "<h2>齐活!功德无量!all done!</h2>";
  75. }
  76. else{
  77. echo "<script>";
  78. echo "window.location.assign(\"db_insert_bookword_from_csv.php?from=".($from+1)."&to=".$to."\")";
  79. echo "</script>";
  80. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  81. }
  82. ?>
  83. </body>
  84. </html>