db_insert_word_from_csv_cli.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. 生成 巴利原文段落表
  4. */
  5. require_once __DIR__."/../config.php";
  6. set_exception_handler(function($e){
  7. fwrite(STDERR,"error-msg:".$e->getMessage().PHP_EOL);
  8. fwrite(STDERR,"error-file:".$e->getFile().PHP_EOL);
  9. fwrite(STDERR,"error-line:".$e->getLine().PHP_EOL);
  10. exit;
  11. });
  12. define("_DB_", _PG_DB_PALI_INDEX_);
  13. define("_TABLE_", _PG_TABLE_WORD_);
  14. fwrite(STDOUT, "Insert Word To DB".PHP_EOL);
  15. if ($argc != 3) {
  16. echo "help".PHP_EOL;
  17. echo $argv[0]." from to".PHP_EOL;
  18. echo "from = 1-217".PHP_EOL;
  19. echo "to = 1-217".PHP_EOL;
  20. exit;
  21. }
  22. $_from = (int) $argv[1];
  23. $_to = (int) $argv[2];
  24. if ($_to > 217) {
  25. $_to = 217;
  26. }
  27. $dirLog = _DIR_LOG_ . "/";
  28. $dirXmlBase = _DIR_PALI_CSV_ . "/";
  29. $filelist = array();
  30. $fileNums = 0;
  31. $log = "";
  32. global $dbh_word_index;
  33. $dns = _DB_;
  34. $dbh_word_index = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  35. $dbh_word_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  36. if (($handle = fopen(__DIR__."/filelist.csv", 'r')) !== false) {
  37. while (($filelist[$fileNums] = fgetcsv($handle, 0, ',')) !== false) {
  38. $fileNums++;
  39. }
  40. }
  41. if ($_to == 0 || $_to >= $fileNums) {
  42. $to = $fileNums ;
  43. }
  44. for ($from=$_from-1; $from < $_to; $from++) {
  45. fwrite(STDOUT, "doing ".($from+1).PHP_EOL);
  46. #删除
  47. $query = "DELETE FROM "._TABLE_." WHERE book = ?";
  48. $stmt = $dbh_word_index->prepare($query);
  49. try{
  50. $stmt->execute(array($from+1));
  51. }catch(PDOException $e){
  52. fwrite(STDERR,$e->getMessage());
  53. continue;
  54. }
  55. if (($fpoutput = fopen(_DIR_CSV_PALI_CANON_WORD_ . "/{$from}_words.csv", "r")) !== false) {
  56. // 开始一个事务,关闭自动提交
  57. $dbh_word_index->beginTransaction();
  58. $query = "INSERT INTO "._TABLE_." ( sn , book , paragraph , wordindex , bold ,created_at,updated_at ) VALUES (?,?,?,?,?,now(),now())";
  59. $stmt = $dbh_word_index->prepare($query);
  60. $count = 0;
  61. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  62. try{
  63. $stmt->execute($data);
  64. }catch(PDOException $e){
  65. fwrite(STDERR,$e->getMessage());
  66. fwrite(STDERR,implode(",",$data));
  67. break;
  68. }
  69. $count++;
  70. }
  71. // 提交更改
  72. $dbh_word_index->commit();
  73. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  74. $error = $dbh_word_index->errorInfo();
  75. fwrite(STDERR, "error - $error[2] ".PHP_EOL);
  76. $log .= "$from, $FileName, error, $error[2] \r\n";
  77. } else {
  78. fwrite(STDOUT, "updata $count recorders.".PHP_EOL);
  79. $log .= "updata $count recorders.\r\n";
  80. }
  81. }
  82. }
  83. fwrite(STDOUT, "齐活!功德无量!all done!".PHP_EOL);
  84. ?>