bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monasterians:
This might fall more into the realm of a MySQL forum, but I'll take a chance here in a familar place.
I wrote a little script that uploads a CSV file and imports it into a table. I do it all the time, but this time it is only importing a few lines. In a larger version of this same CSV file, it imports the first few lines, a couple in the middle, and then the last one.
I've examined it for "gremlins" and find the data clean. Frankly, I'm at a loss. Here's the statement that gets executed, or you can view the whole script below.
$stmt =qq/LOAD DATA LOCAL INFILE "..\/$cvstoupload" INTO TABLE statezones FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES/;
Whole CSV file:
STATE,ABBREV,ZONE Alabama,AL,2 Alaska,AK,4 Arizona,AZ,3 Arkansas,AR,2 California,CA,3 Colorado,CO,2 Connecticut,CT,2 Delaware,DE,2
All that imports:
Alabama,AL,2 Alaska,AK,4 Arizona,AZ,3
Entire script:
#!/usr/bin/perl -T use lib "/home/gotjunk/www/cgi-bin"; use warnings; use CGI::Carp qw(fatalsToBrowser); use strict; use CGI; use Crypt::CBC; require Common; #------------------------- declare -------------------------- my $query = new CGI; my $dbh = &dbconnect; #open DBI connection in Common #------------------------ grab input ------------------------ my $cvstoupload = &upload_file($query->param('filename')); my $stmt ="DELETE FROM statezones"; #dump the old data &execute_it($stmt); #----------------- open .db file and insert ----------------- $stmt =qq/LOAD DATA LOCAL INFILE "..\/$cvstoupload" INTO TABLE statezones FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES/; my $sth = $dbh->prepare($stmt); $sth->execute(); unlink "../$cvstoupload" or die "unlink: $!"; $sth->finish(); $dbh->disconnect(); exit(); # ----------------------- upload file ----------------------- sub upload_file { $| = 1; my $sourcefile = shift; my ($buffer, $bytes); $sourcefile =~ /([\w .-]+)$/i; my $newfile = $1; $newfile =~ s/ //; open (OUTFILE, ">../$newfile") or die "Cannot open $newfile: $!"; binmode(OUTFILE); while ($bytes = read($sourcefile, $buffer, 1024)) { print OUTFILE $buffer; } close(OUTFILE) or die "Close: $!"; chmod (0666, ">../$newfile"); return ($newfile); }
Thanks in advance
UPDATE: After testing all conditions and not finding anything in particular, I rebuilt the table and CSV file and got it to work. Thanks to those who responded.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Unable to load entire CSV file into DB
by cchampion (Curate) on Nov 06, 2004 at 17:22 UTC | |
by bradcathey (Prior) on Nov 06, 2004 at 17:52 UTC | |
by tachyon (Chancellor) on Nov 07, 2004 at 04:13 UTC | |
by bradcathey (Prior) on Nov 07, 2004 at 12:46 UTC | |
by steves (Curate) on Nov 07, 2004 at 13:10 UTC | |
| |
by Anonymous Monk on Nov 06, 2004 at 23:43 UTC | |
Re: Unable to load entire CSV file into DB
by Zaxo (Archbishop) on Nov 06, 2004 at 17:46 UTC | |
by shenme (Priest) on Nov 07, 2004 at 00:34 UTC |