2013-10-24 43 views
1

我正在寫一個perl腳本來更新oracle數據庫中的表與mysql數據庫中的數據。perl使用來自另一個數據庫的值更新oracle數據庫

我是新來的Perl,所以任何幫助,將不勝感激。

我目前有以下不更新oracle數據庫,但也不會引發任何錯誤。

數據庫已經初始化。

我想oracle tblrecommendations表的性能更新mysql tblrecommendations表中的內容。

在此先感謝。

#transfer data 
sub do_crc_company_performance { 

my ($sth_mysql, $sth_oracle); 
my $sql_details = <<END_SQL; 
select 
    tblRecommendations.code, 
    tblRecommendations.performance 
from 
    crc.tblRecommendations 
where 
    length(tblRecommendations.code) = '3' 
END_SQL 

# variables to bind values to 

my ($code, $performance); 

eval { 
    # prepare our select statement for mysql 
    $sth_mysql = $dbh_mysql->prepare($sql_details); 
    $sth_mysql->execute; 
    $sth_mysql->bind_columns(\($code, $performance)); 
    # create oracle insertion query 
    $sth_oracle = $dbh_oracle->prepare(q{UPDATE TBLRECOMMENDATIONS 
             SET PERFORMANCE = '$performance' 
             WHERE CODE = '$code'}); 
    while ($sth_mysql->fetch) { 
     $performance = Encode::decode_utf8($performance); # set the flag 
     # feed the data into the tblRecommendations table 
     $sth_oracle->execute(); 
    } 
}; 

if ([email protected]) { 
    # what went wrong 
    push (@errors, "Unable to update company details: [email protected]"); 
    # rollback our transaction 
    $dbh_oracle->rollback() 
} 
$sth_oracle->finish if ($sth_oracle); 
$sth_mysql->finish if ($sth_mysql); 
} 

回答

3

你的問題是你q{} quoting,這是文字字符串,沒有插報價。因此,您正在搜索code字段設置爲五個字符文字字符串值$code的記錄。

一種解決方案是使用插值引用 - ""qq{}。但是,這很容易導致不愉快的SQL注入,因此很容易造成strongly discouraged

正如您發現的,更好的解決方案是使用bind values並讓RDBMS驅動程序爲您處理引用和轉義。不過,你不需要在這種情況下,中介$某物:

$dbh_ora->do(q{UPDATE tbl SET foo = ? WHERE bar = ?}, undef, $new_foo, $bar); 

現在,我推斷你有RAISEERROR集,你不關心更新的行數,所以你(好!)甚至不需要將該呼叫的返回值捕獲到do()

+0

感謝這工作:) – JordanC

0

我沒有在您的代碼中看到COMMIT,這是使您的更改永久化所必需的。某處存在(無論是後每次插入或取環後)你想:

$sth_oracle->commit;

+0

自動提交已啓用。每次腳本運行時,我都會刪除提交以擺脫警告消息。 – JordanC

+0

找到了一種方法來做到這一點。 「$ sth_oracle = $ dbh_oracle-> do('UPDATE tblrecommendations SET performance =?,updated =?WHERE code =?',undef,$ performance,$ updated,$ code);」它可能不是最好的,但它的工作原理。明天我會回答我自己的問題。 – JordanC

0

對於任何對我的最終解決方案感興趣的人,在這裏。

sub do_crc_company_performance { 

my ($sth_mysql, $sth_oracle); 
my $sql_details = <<END_SQL; 
select 
    tblRecommendations.code, 
    tblRecommendations.performance 
from 
    crc.tblRecommendations 
where 
    length(tblRecommendations.code) = '3' 
END_SQL 


# variables to bind values to 
my ($code, $performance); 

eval { 

    # prepare our select statement for mysql 
    $sth_mysql = $dbh_mysql->prepare($sql_details); 
    $sth_mysql->execute; 
    $sth_mysql->bind_columns(\$code, \$performance); 
    # create oracle insertion query 

    while ($sth_mysql->fetch) { 
     $performance = Encode::decode_utf8($performance); # set the flag 
     # feed the data into the tblRecommendations table 
     $sth_oracle = $dbh_oracle->do('UPDATE tblrecommendations SET performance = ? WHERE code = ?', undef, $performance, $code); 
    } 
}; 

if ([email protected]) { 
    # what went wrong 
    push (@errors, "Unable to update company details: [email protected]"); 
    # rollback our transaction 
} 

}

相關問題