db_insert_sim.php 922 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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("sqlite:$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. }
  31. else{
  32. echo "can not open csv file. ";
  33. }
  34. // 提交更改
  35. $PDO->commit();
  36. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  37. $error = PDO_ErrorInfo();
  38. echo " error, $error[2] \r\n";
  39. }
  40. else{
  41. echo "updata $count recorders.";
  42. }
  43. ?>