db_insert_word_from_csv.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 = $_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 = "" . _FILE_DB_PALI_INDEX_;
  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) {
  41. $to = $fileNums - 1;
  42. }
  43. if (($fpoutput = fopen(_DIR_CSV_PALI_CANON_WORD_ . "/{$from}_words.csv", "r")) !== false) {
  44. // 开始一个事务,关闭自动提交
  45. $dbh_word_index->beginTransaction();
  46. $query = "INSERT INTO word ('id','book','paragraph','wordindex','bold') VALUES (?,?,?,?,?)";
  47. $stmt = $dbh_word_index->prepare($query);
  48. $count = 0;
  49. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  50. $stmt->execute($data);
  51. $count++;
  52. }
  53. // 提交更改
  54. $dbh_word_index->commit();
  55. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  56. $error = $dbh_word_index->errorInfo();
  57. echo "error - $error[2] <br>";
  58. $log .= "$from, $FileName, error, $error[2] \r\n";
  59. } else {
  60. echo "updata $count recorders.<br />";
  61. $log .= "updata $count recorders.\r\n";
  62. }
  63. }
  64. $myLogFile = fopen($dirLog . "insert_index.log", "a");
  65. fwrite($myLogFile, $log);
  66. fclose($myLogFile);
  67. if ($from >= $to) {
  68. echo "<h2>齐活!功德无量!all done!</h2>";
  69. } else {
  70. echo "<script>";
  71. echo "window.location.assign(\"db_insert_word_from_csv.php?from=" . ($from + 1) . "&to=" . $to . "\")";
  72. echo "</script>";
  73. echo "正在载入:" . ($from + 1) . "——" . $filelist[$from + 1][0];
  74. }
  75. ?>
  76. </body>
  77. </html>