Friday, March 22, 2019

How to import a large CSV in MySQL table

I have struggled for a while to import my large CSV (comma, separated, values) files in my apps.
The common method should be using:
LOAD DATA LOCAL INFILE 'myfile.csv' INTO TABLE table
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(field1, field2, field3, ....);
Seem to be so nice and simple so far, until receiving errors encountered by MySQL versions or incompatibility with the server acceptance (disabled functions).
I am recommending the next method that really worked:
Insert to the database line-by-line of CSV with this PHP file run by Command Prompt:
insert.php:
<?php  
include 'config.php';
ini_set('max_execution_time', 0);
if (($handle = fopen("mycsvfile.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
   $c2=str_replace("'","\'", $data[2]);
       $sql = "INSERT my_table (field1, field2) 
   values('val1','val2')";
   mysql_query($sql,$link) or die("Insertion Failed:" . mysql_error());
    }
    fclose($handle);
}
?>

No comments:

Post a Comment