db_insert_wordindex_from_csv_cli.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /*
  3. 生成 巴利原文段落表
  4. */
  5. require_once __DIR__."/../config.php";
  6. require_once __DIR__.'/../public/_pdo.php';
  7. echo "Insert Word Index To DB".PHP_EOL;
  8. $dirLog = _DIR_LOG_ . "/";
  9. $log = "";
  10. $dns = _FILE_DB_WORD_INDEX_;
  11. $dbh_word_index = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  12. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  13. #删除
  14. $query = "DELETE FROM "._TABLE_WORD_INDEX_." WHERE true";
  15. $stmt = $dbh_word_index->prepare($query);
  16. $stmt->execute();
  17. $scan = scandir(_DIR_CSV_PALI_CANON_WORD_INDEX_);
  18. foreach($scan as $filename) {
  19. $filename = _DIR_CSV_PALI_CANON_WORD_INDEX_."/".$filename;
  20. if (is_file($filename)) {
  21. echo "doing ".$filename.PHP_EOL;
  22. if (($fpoutput = fopen($filename, "r")) !== false) {
  23. // 开始一个事务,关闭自动提交
  24. $dbh_word_index->beginTransaction();
  25. $query = "INSERT INTO "._TABLE_WORD_INDEX_." (id , word , word_en , count , normal , bold , is_base , len ) VALUES (?,?,?,?,?,?,?,?)";
  26. $stmt = $dbh_word_index->prepare($query);
  27. $count = 0;
  28. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  29. $stmt->execute($data);
  30. $count++;
  31. }
  32. // 提交更改
  33. $dbh_word_index->commit();
  34. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  35. $error = $dbh_word_index->errorInfo();
  36. echo "error - $error[2]".PHP_EOL;
  37. $log .= "$filename, error, $error[2] \r\n";
  38. } else {
  39. echo "updata $count recorders.".PHP_EOL;
  40. $log .= "updata $count recorders.\r\n";
  41. }
  42. }else{
  43. echo "open file error".PHP_EOL;
  44. }
  45. }
  46. }
  47. echo "齐活!功德无量!all done!".PHP_EOL;
  48. ?>