db_insert_word_from_csv.php 2.1 KB

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