db_insert_bookword_from_csv.php 2.4 KB

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