I am trying to upload CSV file in to a mysql database using perl. My perl script will reside on a server, which I will access through my browser. In the browser, there will be an option to upload a file.
Question is, how exactly I am gonna do this
- Do I need to save that file somewhere on the server and then use it ?
- Can I directly use the file from my laptop by giving a fixed path ?
so far I tried doing this
if($update eq开发者_开发技巧 "fullLoad"){
$filename = param("customPricing");
my $upload_filehandle = upload("customPricing");
open ( UPLOADFILE, ">$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
$query = "LOAD DATA INFILE '\bigmac\bm_src\html\cgi-bin\testbrocade\$filename' INTO TABLE customPricingTest FIELDS TERMINATED BY ','";
$sth = $dbh->do($query) or die "SQL Error: $DBI::errstr\n";
print $sth . "Records Uploaded <br/>";
}
Edited --> Above code is throwing error Access Denied for user . I am successfully able to create the file on the server but looks like I am getting error accessing it..Any ideas ?
Thanks, Nitesh
In answer to question 1: if you're using LOAD DATA INFILE
, yes, the file needs to be accessible to the server system and database user. If you use LOAD DATA LOCAL INFILE
, the file can stay on a separate system, run by the Perl script, and data is uploaded in the network connection. There are permissions and settings differences between the two, but performance is very good either way, and we didn't see a huge difference whether loading the data locally or remotely.
For the second question, this is what LOAD DATA LOCAL INFILE
is for. Here, because the file is read and passed through the DBI connection, the file path needs to be relative to where the script is being run. It doesn't need to be absolute in the way that it would for LOAD DATA INFILE
. It doesn't even need to be a fixed path. We've used temporary files for this purpose successfully, so long as the files are not deleted until after you have disconnected from the database.
精彩评论