我正在编写一个 perl 脚本,以使用 mysql 数据库中的数据更新 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 ($@) {
# what went wrong
push (@errors, "Unable to update company details: $@");
# rollback our transaction
$dbh_oracle->rollback()
}
$sth_oracle->finish if ($sth_oracle);
$sth_mysql->finish if ($sth_mysql);
}
最佳答案
你的问题是你的q{}
quoting ,这是没有插值的文字字符串引用。因此,您正在搜索 code
字段设置为五个字 rune 字字符串值 $code
的记录。
一种解决方案是使用插值来引用 - ""
或 qq{}
。然而,这很容易导致令人不快的 SQL 注入(inject),因此 strongly discouraged .
正如您发现的,更好的解决方案是使用 bind values并让 RDBMS 驱动程序为您处理引用和转义。但是,在这种情况下,您不需要中介 $sth:
$dbh_ora->do(q{UPDATE tbl SET foo = ? WHERE bar = ?}, undef, $new_foo, $bar);
现在,我推断您已经设置了 RaiseError(很好!),并且您不关心 UPDATEd 的行数,因此您甚至不需要捕获对 do 调用的返回值()
.
关于mysql - Perl 使用另一个数据库中的值更新 Oracle 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19554768/