Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV file into the MySQL database

I have records into .CSV file and I want to import them into MySQL database.

Whenever I import the .CSV I get the message Import has been successfully finished... but only 79 out of 114 records are be inserted into the database.

When I try to import the .CSV file with 411 records, just 282 are be inserted. The CSV file which got 411 records includes two categories of records Active and Sold whereby 114 records are Active.

Has someone gotten this type of problem? If so what should be done?

like image 885
user1133158 Avatar asked Aug 01 '12 20:08

user1133158


2 Answers

I wrote my own csv importer with php. I use php command fgetcsv to read the csv file and then I use mysql insert command in a loop.

$handle = fopen($this->file, "r");
$i=0;
$delimiter = ($this->fieldDelimiter == 'TAB') ? chr(9) : $this->fieldDelimiter;
while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE)
{
     $mydata[] = $data;
}
fclose ($handle);
reset ($mydata);
if ($this->CSVhasTitle)
{
      $mydata = array_slice($mydata,1); //delete first row
}

Then I loop through my array and I use mysql insert:

foreach ($mydata as $value) 
{
     INSERT INTO $table (...) VALUES (....)
} 

But I add exact columnnames into the array before the loop. I've an array of all columnames.

like image 192
Micromega Avatar answered Sep 30 '22 09:09

Micromega


I had this problem too. Even though its a bit old and these recommendations go in one direction here is the solution I found. I was creating a large database and import and had the same thing happen, after trial and trials I realized that a key was created somehow assigned (I didn't recognize it because Iwas using the new skin and always use the old skin). Took out the key that somehow got assigned not by me and left the primary only and boom, absorbed the data upload. Also, had issues with view now, and request times out with respect to your question. I pushed up the viewer display a lot to thousands and now stuck, cannot access the config file anywhere with my CP. So as will hang and hosting customer support to lazy to read my concerns and override it on their end-I will have to remove whole table instead of any DROP as can't even run SQL as freezes with overload. So, food for thought would be to keep you table view down, which sucks like in my case b/c I need to look at 17,000 rcords visually quickly to ensure my .csv was correct rather then functions as if issues then can spot them and correct in the control which makes more sense to me anyway.

like image 40
user1594629 Avatar answered Sep 30 '22 10:09

user1594629