2013-10-24 45 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 
} 

}

相关问题