mysql - Perl 使用另一个数据库中的值更新 Oracle 数据库

标签 mysql database oracle perl dbi

我正在编写一个 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/

相关文章:

mysql - 复杂的 sum() 与组和按日期排序

mysql - 无法在 mysql(phpmyadmin) 中获取与 0 不同的右侧小数值

mysql - 用于检索具有相同名称和最近创建的记录的 SQL 查询

oracle - Oracle 对象的使用范围有多广?

oracle - 如何仅显示错误消息Oracle引发应用程序错误

mysql - SQL 仅选择列上具有最大值的行

php - 新表(table1 join table2)中有两列具有相同的列名,如何分别获取两者的值

mysql - 在范围之间添加每个月的预订天数

php - 从多个表复制列值并插入 Laravel 中的第三个表

sql - Oracle SQL Sum 缺少右括号