db_insert_word_from_csv.php 2.1 KB

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