简体   繁体   English

将sqlplus命令的输出保存到数组中的任何可能方法-Perl

[英]Any possible way to save output of sqlplus command into an array - Perl

Currently, I am spooling the sqlplus command into a text file but even this is causing me problems as I would like to separate the values by commas. 当前,我正在将sqlplus命令假脱机到一个文本文件中,但是即使这样也会给我造成麻烦,因为我想用逗号分隔值。 So far It has not worked. 到目前为止,它没有起作用。

I was hoping something like this would work 我希望这样的事情能奏效

 @test = system('sqlplus un/pw@host @test.sql');

The test.sql file contains three statements all returning numbers. test.sql文件包含三个返回数字的语句。 If I could save these to the @test array, this would be great. 如果我可以将它们保存到@test数组,那就太好了。

Any ideas? 有任何想法吗?

Use the DBI module with DBD::Oracle : DBI模块与DBD :: Oracle一起使用

#!/usr/bin/perl
use warnings;
use strict;

use DBI;

my $dbh = DBI->connect("dbi:Oracle:host=host;sid=dbname",
                       $user, $password);

open my $IN, '<', 'test.sql' or die $!;
$/ = ';'; # Queries separated by semicolons, no other semicolons anywhere!
while (my $sql = <$IN>) {
    my $sth = $dbh->prepare($sql);
    $sth->execute;
    my @test = @{ $sth->fetchall_arrayref // [] };
    print "@$_\n" for @test;
}

The DBI script based is good, but if your point is about the use of sqlplus command in any case, you should take a look at this CPAN module: Expect . 基于DBI脚本是很好的,但是无论如何,如果您要使用sqlplus命令,则应该看一下以下CPAN模块: Expect It provides you a deeper control of an external command in many and useful ways. 它以许多有用的方式为您提供了对外部命令的更深入的控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM