我正在编写一个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);
}
答案 0 :(得分:3)
你的问题是你的q{}
quoting,它是文字字符串引用而没有插值。因此,您正在搜索code
字段设置为五个字符的字符串值$code
的记录。
一种解决方案是引用插值 - ""
或qq{}
。但是,这很容易导致令人不快的SQL注入,因此strongly discouraged。
正如您所发现的,更好的解决方案是使用bind values并让RDBMS驱动程序为您处理引用和转义。但是,在这种情况下,您不需要中间$ sth:
$dbh_ora->do(q{UPDATE tbl SET foo = ? WHERE bar = ?}, undef, $new_foo, $bar);
现在,我推断您已经设置了RaiseError(好!),并且您不关心UPDATEd的行数,因此您甚至不需要捕获该调用的返回值{{1} }。
答案 1 :(得分:1)
对于对我有用的最终解决方案感兴趣的人,请点击这里。
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 ($@) {
# what went wrong
push (@errors, "Unable to update company details: $@");
# rollback our transaction
}
}
答案 2 :(得分:0)
我没有在您的代码中看到COMMIT,这是使您的更改永久保留所必需的。在那里的某个地方(在每次插入之后或在获取循环之后)你想要:
$sth_oracle->commit;