使用Perl更新MySQL数据库时出现504 Gateway错误

时间:2011-08-02 21:24:33

标签: mysql perl

所有

我正在尝试使用perl脚本更新mysql数据库。此脚本允许用户上载csv文件,csv中的记录将上载到数据库中。我一直在运行504网关错误。

有什么想法吗?

#!/usr/bin/perl --

# database information
$db="database";
$host="localhost";
$port="3306";
$userid="userName";
$passwd="Password";
$connectionInfo="DBI:mysql:database=$db;$host:$port";

use CGI qw(:cgi); # possible values: (cgi, form, html, html2, html3, netscape, standard, all)
my $cgi = new CGI; # create CGI-Object
use CGI::Carp qw(fatalsToBrowser); # redirect error messages to browser output
print "Content-type: text/html\n\n";
use DBI;
use Switch;

$dbh = DBI->connect($connectionInfo,$userid,$passwd) or die "Could not establish database connection.";

$update = param("upd");

$currentRecord = 0;
$error = 0;

if ( $update eq "upsertIncremental" ) {
    $filename = param("customPricing");
    $handle = upload("customPricing");
    while ( <$handle> )  {              
        $currentRecord++;
        @currentLine = split( /,/, $_ );
        $i = 0;
        foreach $l (@currentLine) {
            $currentLine[$i] =~ s/\\r//g; 
            $i++;
        }

            $query = "UPDATE customPricing
                    SET partNumberSKU='$currentLine[0]', customerClass='$currentLine[1]', customerName='$currentLine[2]', customerId='$currentLine[3]', customerNumber='$currentLine[4]', custPartNumber='$currentLine[5]', svcType='$currentLine[6]', sppl='$currentLine[7]', svcDuration='$currentLine[8]', durationPeriod='$currentLine[9]', priceMSRP='$currentLine[10]', partnerPriceDistiDvarOEM='$currentLine[11]', msrpSvcPrice='$currentLine[12]', partnerSvcPrice='$currentLine[13]', msrpBundlePrice='$currentLine[14]', partnerBundlePrice='$currentLine[15]', startDate='$currentLine[16]', endDate='$currentLine[17]', currency='$currentLine[18]', countryCode='$currentLine[19]', inventoryItemId='$currentLine[20]', flexField1='$currentLine[21]', flexField2='$currentLine[22]', flexField3='$currentLine[23]', flexField4='$currentLine[24]', flexField5='$currentLine[25]'
                    WHERE partNumberSKU='$currentLine[0]' and ifnull(customerClass,0)=ifnull('$currentLine[1]',0) and ifnull(customerName,0)=ifnull('$currentLine[2]',0) and ifnull(svcType,0)=ifnull('$currentLine[6]',0) and ifnull(svcDuration,0)=ifnull('$currentLine[8]',0) and ifnull(durationPeriod,0)=ifnull('$currentLine[9]',0)";
            $sth = $dbh->prepare($query) or $error = 1;
            $rv = $sth->execute() or die "can't execute UPDATE  query. \n";



    }
    print $currentRecord . " Record(s) uploaded.<br/>";
    $dbh->disconnect;   
}



print qq~
Please select a comma-delimited text file to upload below:<br>

<input type="radio" name="upd" value="upsert">Upsert<br/>
<input type="file" name="customPricing">
<br/>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
</form>

<br><em>Note: If you are using Excel, save your worksheet by going to File->Save As, then selecting "CSV (.csv)" from the "Save as type" pull-down. The data must conform to the template set by the sample contacts list below.</em><br>
<br>

谢谢, Nitesh

我也试过这种方法

 $filename = param("customPricing");
    $handle = upload("customPricing");

        while ( <$handle> )  {              
        $currentRecord++;
        @currentLine = split( /,/, $_ );


        $update_handle = $dbh->prepare_cached("UPDATE custompricingtest SET partNumberSKU= ?, customerClass= ?, customerName= ?, customerId= ?, customerNumber= ?, custPartNumber=?, svcType= ?, sppl= ? , svcDuration= ?, durationPeriod= ?, priceMSRP= ?, partnerPriceDistiDvarOEM= ?, msrpSvcPrice=?, partnerSvcPrice=?, msrpBundlePrice=?, partnerBundlePrice=?, startDate=?, endDate=?, currency=?, countryCode=?, inventoryItemId=?, flexField1=?, flexField2=?, flexField3=?, flexField4=?, flexField5=? WHERE partNumberSKU=? and ifnull(customerClass,0)=ifnull(?,0) and ifnull(customerName,0)=ifnull(?,0) and ifnull(svcType,0)=ifnull(?,0) and ifnull(svcDuration,0)=ifnull(?,0) and ifnull(durationPeriod,0)=ifnull(?,0)") or $error = 1;

        $update_handle->execute($currentLine[0],$currentLine[1],$currentLine[2],$currentLine[3],$currentLine[4],$currentLine[5],$currentLine[6],$currentLine[7],$currentLine[8],$currentLine[9],$currentLine[10],$currentLine[11],$currentLine[12],$currentLine[13],$currentLine[14],$currentLine[15],$currentLine[16],$currentLine[17],$currentLine[18],$currentLine[19],$currentLine[20],$currentLine[21],$currentLine[22],$currentLine[23],$currentLine[24],$currentLine[25],$currentLine[0],$currentLine[1],$currentLine[2],$currentLine[6],$currentLine[8],$currentLine[9]) or die "can't execute UPDATE  query. \n";


    }
    print $currentRecord . " Record(s) uploaded.<br/>";
    $dbh->disconnect;   

即使那时它运行速度超慢,我也会错过时间。

0 个答案:

没有答案