简体   繁体   English

将大型CSV文件上传到Mysql数据库

[英]Uploading Large CSV File into Mysql Database

I want to upload large CSV document into mysql database can anyone help me out, the table consist of 10 fields but i want to upload into only 5 fields of the table without a heading. 我想将大型CSV文档上载到mysql数据库中,任何人都可以帮助我,该表包含10个字段,但是我想仅上载到表的5个字段中而不使用标题。 I want this done with php in the browser 我想用浏览器中的php完成此操作

$filename = $_FILES['sel_file']['tmp_name'];
<?php
    if($_FILES["file"]["type"] != "application/vnd.ms-excel"){
       die("This is not a CSV file.");
    }
    elseif(is_uploaded_file($_FILES['file']['name'])){
     $dbhost = 'localhost';
     $dbuser = 'root';
     $dbpass = '';
     $dbname = 'cbt_software';
     $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error        connecting to mysql server');
   mysql_select_db('cbt_software') or die(mysql_error());
   //Process the CSV file
   $handle = fopen($_FILES['file']['name'], "r");
   $data = fgetcsv($handle, 1000, ";"); 
   while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
      $att0 = mysql_real_escape_string($data[0]);
      $att1 = mysql_real_escape_string($data[1]);
      $att2 = mysql_real_escape_string($data[2]);
      $att3 = mysql_real_escape_string($data[3]);
      $att4 = mysql_real_escape_string($data[4]);  

      $sql = "INSERT INTO `course_reg` (`coursecode`,`coursename`,`coursedescription`,`coursemaster`,`courselevel`)VALUES     ('$att0','$att1','$att2','$att3','$att4')";
      mysql_query($sql) or die(mysql_error());
   }
   mysql_close($link);
   echo "CSV file successfully imported.";
}
else{
   die("You shouldn't be here");
}
?>

At first this imported all the field from the csv into just one field in the database and after i tampered with the code it is not recognicing it asa CSV file. 起初,这将所有字段从csv导入数据库中的一个字段,并且在我篡改了代码之后,它无法将其识别为CSV文件。

If you have the appropriate permissions, you can do so directly in MySQL with the LOAD DATA INFILE command, see http://dev.mysql.com/doc/refman/4.1/en/load-data.html or the mysqlimport utility, see http://dev.mysql.com/doc/refman/4.1/en/mysqlimport.html 如果您具有适当的权限,则可以使用LOAD DATA INFILE命令直接在MySQL中执行此操作,请参见http://dev.mysql.com/doc/refman/4.1/en/load-data.htmlmysqlimport实用程序,参见http://dev.mysql.com/doc/refman/4.1/en/mysqlimport.html

Both methods will allow you to specify which columns the data should go in, for instance: 两种方法都允许您指定数据应进入的列,例如:

LOAD DATA INFILE 'myfile.txt' INTO TABLE 'mytable' (col1, col2, col3, ...)

or 要么

mysqlimport --columns='col1,col2,...' tablename.csv

If you intend to do it from PHP, you should be able to read each line of the CSV file and execute an appropriate SQL INSERT query naming the appropriate columns (although that will not be as efficient as doing it directly in MySQL). 如果打算从PHP进行操作,则应该能够读取CSV文件的每一行并执行适当的SQL INSERT查询,为相应的列命名(尽管这样做的效率不如直接在MySQL中执行)。

EDIT: I should add that you haven't mentioned what you've tried so far or what you're finding difficult; 编辑:我应该补充一点,您还没有提到您到目前为止已经尝试过的内容或发现困难的内容; if you're stuck on something in particular, rather than just looking for suggestions on how to go about doing it, please update the question to say so. 如果您特别专注于某些事情,而不仅仅是在寻求有关如何做的建议,请更新问题以这样说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM