db_insert_word_from_csv.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. require_once "install_head.php";
  3. ?>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. </head>
  8. <body>
  9. <h2>Insert csv to word table</h2>
  10. <p><a href="index.php">Home</a></p>
  11. <?php
  12. include "./_pdo.php";
  13. if (isset($_GET["from"]) == false) {
  14. ?>
  15. <form action="db_insert_word_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 = (int)$_GET["from"];
  24. $to = (int)$_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 = _FILE_DB_PALI_INDEX_;
  33. $dbh_word_index = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, 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) {
  41. $to = $fileNums - 1;
  42. }
  43. #删除
  44. $query = "DELETE FROM "._TABLE_WORD_." WHERE book = ?";
  45. $stmt = $dbh_word_index->prepare($query);
  46. $stmt->execute(array($from+1));
  47. if (($fpoutput = fopen(_DIR_CSV_PALI_CANON_WORD_ . "/{$from}_words.csv", "r")) !== false) {
  48. // 开始一个事务,关闭自动提交
  49. $dbh_word_index->beginTransaction();
  50. $query = "INSERT INTO "._TABLE_WORD_." ( sn , book , paragraph , wordindex , bold ) VALUES (?,?,?,?,?)";
  51. $stmt = $dbh_word_index->prepare($query);
  52. $count = 0;
  53. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  54. $stmt->execute($data);
  55. $count++;
  56. }
  57. // 提交更改
  58. $dbh_word_index->commit();
  59. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  60. $error = $dbh_word_index->errorInfo();
  61. echo "error - $error[2] <br>";
  62. $log .= "$from, $FileName, error, $error[2] \r\n";
  63. } else {
  64. echo "updata $count recorders.<br />";
  65. $log .= "updata $count recorders.\r\n";
  66. }
  67. }
  68. $myLogFile = fopen($dirLog . "insert_index.log", "a");
  69. fwrite($myLogFile, $log);
  70. fclose($myLogFile);
  71. if ($from >= $to) {
  72. echo "<h2>齐活!功德无量!all done!</h2>";
  73. } else {
  74. echo "<script>";
  75. echo "window.location.assign(\"db_insert_word_from_csv.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  76. echo "</script>";
  77. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  78. }
  79. ?>
  80. </body>
  81. </html>