db_insert_sim.php 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /*
  3. 用相似句导入数据库
  4. */
  5. ?>
  6. <?php
  7. require_once '../public/_pdo.php';
  8. require_once '../path.php';
  9. $filelist = array();
  10. $fileNums = 0;
  11. $log = "";
  12. echo "start\n";
  13. $db_file = _DIR_PALICANON_ . "/pali_sim.db3";
  14. PDO_Connect("$db_file");
  15. // 开始一个事务,关闭自动提交
  16. $PDO->beginTransaction();
  17. $query = "INSERT INTO sent_sim ('sent1','sent2','sim') VALUES (? , ? , ?)";
  18. $stmt = $PDO->prepare($query);
  19. // 打开文件并读取数据
  20. $count = 0;
  21. if (($fp = fopen(_DIR_TMP_ . "/pali_simsent/sim.csv", "r")) !== false) {
  22. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  23. $stmt->execute($data);
  24. $count++;
  25. if ($count % 1000000 == 0) {
  26. echo $count . "\n";
  27. }
  28. }
  29. fclose($fp);
  30. } else {
  31. echo "can not open csv file. ";
  32. }
  33. // 提交更改
  34. $PDO->commit();
  35. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  36. $error = PDO_ErrorInfo();
  37. echo " error, $error[2] \r\n";
  38. } else {
  39. echo "updata $count recorders.";
  40. }