【问题标题】:Fastest way to import csv file into MYSQL将csv文件导入MYSQL的最快方法
【发布时间】:2015-06-09 07:11:11
【问题描述】:

此方法将 csv 文件上传到 mysql 。但是由于 csv 文件中有数千个数据,上传数据需要花费大量时间,这很烦人。

        $deleterecords = "TRUNCATE TABLE discount"; //empty the table of its current records
        mysql_query($deleterecords);
        //readfile($name);
        //Import uploaded file to Database
        $handle = fopen($name, "r");

        $i=0;
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($i>0){
            $import="INSERT into discount(id,title,expired_date,amount,block)values('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."')";
        //imports data serially to the allocated columns.
            mysql_query($import) or die(mysql_error());//query
        }
        $i=1;
        }
        fclose($handle);
       //closing the handle
       // print "Import done ";
       ?>

Can anyone suggest faster method for uploading data ?

【问题讨论】:

标签: php mysql csv


【解决方案1】:

使用 LOAD DATA INFILE 语句。 https://dev.mysql.com/doc/refman/5.1/en/load-data.html

将数据加载到临时表中,并使用一条语句将其插入到主表中。

【讨论】:

    【解决方案2】:

    您可以直接将 MYSQL 链接到它并使用以下 SQL 语法上传信息,而不是编写脚本来从 CSV 文件中提取信息。

    要将 Excel 文件导入 MySQL,请先将其导出为 CSV 文件。从生成的 CSV 文件中删除 CSV 标头以及 Excel 可能放在 CSV 文件末尾的空数据。

    然后您可以通过运行将其导入 MySQL 表:

    load data local infile 'uniq.csv' into table tblUniq fields terminated by ','
      enclosed by '"'
      lines terminated by '\n'
        (uniqName, uniqCity, uniqComments)
    

    继续阅读:Import CSV file directly into MySQL

    【讨论】:

      【解决方案3】:

      您可以通过这种方式插入数据。这是在表中插入行的默认方式。

          $deleterecords = "TRUNCATE TABLE discount"; //empty the table of its current records
          mysql_query($deleterecords);
          //readfile($name);
      
          //Import uploaded file to Database
          $handle = fopen($name, "r");
      
          $i=0;
      
          $ins = "INSERT into discount(id,title,expired_date,amount,block) values ";
      
          while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      
          if($i>0){
      
          $import .= $ins."('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."'),";
          //imports data serially to the allocated columns.
      
          }
      
          $import = rtrim($import,',');
          mysql_query($import) or die(mysql_error());//query
          $i=1;
          }
          fclose($handle);
         //closing the handle
         // print "Import done ";
         ?>
      

      【讨论】:

      • 你应该去掉 $import 字符串末尾的最后一个逗号,在 mysql_query() 之前。
      【解决方案4】:

      不要有多个插入,而是构建一个大查询并执行一个插入。

      <?php
      $deleterecords = "TRUNCATE TABLE discount"; //empty the table of its current records
      mysql_query($deleterecords);
      //readfile($name);
      //Import uploaded file to Database
      $handle = fopen($name, "r");
      
      $i=0;
      $what_to_insert = array();
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          if($i>0){
              array_push($what_to_insert, "('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."')");
          }
          $i=1;
      }
      fclose($handle);
      if (count($what_to_insert)>0){
          $import="INSERT into discount(id,title,expired_date,amount,block) values " . implode(",", $what_to_insert);
          mysql_query($import) or die(mysql_error());//query
      }
      ?>
      

      【讨论】:

        【解决方案5】:

        如果 phpMyAdmin 可用,您可以使用 CSV 导入功能。

        【讨论】:

          猜你喜欢
          • 2022-07-25
          • 2011-01-03
          • 2022-07-10
          • 2013-10-15
          • 1970-01-01
          • 2017-10-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多