db_insert_wordindex_from_csv.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <body>
  6. <h2>Insert to Index</h2>
  7. <?php
  8. include "./_pdo.php";
  9. include "../path.php";
  10. if(isset($_GET["from"])==false){
  11. $from=0;
  12. }
  13. else{
  14. $from=$_GET["from"];
  15. }
  16. $dirLog=_DIR_LOG_."/";
  17. $filelist=array();
  18. $fileNums=0;
  19. $log="";
  20. echo "<h2>doing : No.{$from} </h2>";
  21. global $dbh_word_index;
  22. $dns = "sqlite:"._FILE_DB_WORD_INDEX_;
  23. $dbh_word_index = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  24. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  25. if(($fpoutput=fopen(_DIR_CSV_PALI_CANON_WORD_INDEX_."/{$from}.csv", "r"))!==FALSE){
  26. // 开始一个事务,关闭自动提交
  27. $dbh_word_index->beginTransaction();
  28. $query="INSERT INTO wordindex ('id','word','word_en','count','normal','bold','is_base','len') VALUES (?,?,?,?,?,?,?,?)";
  29. $stmt = $dbh_word_index->prepare($query);
  30. $count=0;
  31. while(($data=fgetcsv($fpoutput,0,','))!==FALSE){
  32. $stmt->execute($data);
  33. $count++;
  34. }
  35. // 提交更改
  36. $dbh_word_index->commit();
  37. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  38. $error = $dbh_word_index->errorInfo();
  39. echo "error - $error[2] <br>";
  40. $log.="$from, $FileName, error, $error[2] \r\n";
  41. }
  42. else{
  43. echo "updata $count recorders.<br />";
  44. $log.="updata $count recorders.\r\n";
  45. }
  46. }
  47. else{
  48. echo "<h2>齐活!功德无量!all done!</h2>";
  49. exit;
  50. }
  51. echo "<script>";
  52. echo "window.location.assign(\"db_insert_wordindex_from_csv.php?from=".($from+1)."\")";
  53. echo "</script>";
  54. echo "正在载入:".($from+1)."——".$filelist[$from+1][0];
  55. ?>
  56. </body>
  57. </html>