使用带有foreach的perl从数据库中检索数据

时间:2011-03-11 03:35:51

标签: sql oracle perl

$dbh_source2 = DBI->connect("dbi:Oracle:host=.......;port=......;sid=......",'..........','..........');
foreach $data_line (@raw_data) {
    $SEL = "SELECT arg1,arg2 FROM TABLE_NAME WHERE DATA_NAME = '$data_line'";
    $sth = $dbh_source2->prepare($SEL);
    $sth->execute();

    while (my @row = $sth->fetchrow_array() ) {
        print @row;
        print "\n";
    }
}
END {
    $dbh_source2->disconnect if defined($dbh_source2);
}

我正试图从用户那里获取几行数据。我想获取该数据并使用它来查询数据库并抓取ARG1ARG2 WHERE USER_DATA = $data_line

它不会显示任何内容。

2 个答案:

答案 0 :(得分:0)

这是一个使用SQL占位符的快速修订版,以防止Bobby Tables破坏您的数据库。它也可以解决你目前遇到的问题,但我还没有看到你问题的足够详细信息。

my $dbh_source2 = DBI->connect("dbi:Oracle:host=.......;port=......;sid=......",'..........','..........');

my $SEL = "SELECT arg1,arg2 FROM TABLE_NAME WHERE DATA_NAME = ?";
my $sth = $dbh_source2->prepare($SEL);

foreach my $data_line (@raw_data) {
    $sth->execute($data_line);

    while (my @row = $sth->fetchrow_array() ) {
        print @row;
        print "\n";
    }
}
END {
    $dbh_source2->disconnect if defined($dbh_source2);
}

答案 1 :(得分:0)

my $dbh_source2 = DBI->connect
("dbi:Oracle:host=.......;port=......;sid=......",'..........','..........');

my $SEL = "SELECT arg1,arg2 FROM TABLE_NAME WHERE DATA_NAME = ?";
my $sth = $dbh_source2->prepare($SEL);

foreach my $data_line (@raw_data) {
    chomp $data_line;
    $sth->execute($data_line);

    while (my @row = $sth->fetchrow_array() ) {
        print "$data_line\t @row\n";
    }
}
END {
    $dbh_source2->disconnect if defined($dbh_source2);
}

我遇到的问题是开发数据库没有使用正确的信息进行更新,因此有些项目是空白的。当使用具有正确信息的生产数据库时,它工作得很好!

谢谢大家的帮助!