【问题标题】:How to handle error thrown by module in perlperl中如何处理模块抛出的错误
【发布时间】:2014-05-31 01:00:36
【问题描述】:

我在 perl 中使用模块 DBD::Oracle 将 xml 内容插入到 oracle 11 g 实例中。在我的示例集中插入一些文档时,脚本失败,因为模块返回Unsupported named object type for bind parameter。我想处理这个错误并继续循环迭代。

以下是我的代码,

use strict;
use warnings;
use DBI;
use DBD::Oracle qw(:ora_session_modes);
use DBD::Oracle qw(:ora_types);

die("USAGE: $0 <input_directory>") unless ($#ARGV == 0);
my $directory=$ARGV[0];

my $dbh = DBI->connect('dbi:Oraclle:dbname',"username", "pass");
my $SQL;



opendir(IMD, $directory) || die ("Cannot open directory");
my @listOfFiles= readdir(IMD);
closedir(IMD);

my $xmltype_string;
my $xml;
my $i = 1; 
foreach my $file(@listOfFiles)
{
    unless($file eq '.' or $file eq '..')
    {
        print "inserting File no. $i \t $file .... \n";

        {
                local $/=undef;
                open (FILE , "<" , "$directory/$file" );
                $xml=<FILE>;
                close (FILE);
        }
        $SQL="insert into sampleTable values ( :ind, :xml)";
        my $sth =$dbh-> prepare($SQL);
        $sth->bind_param(":xml" , $xml , { ora_type => ORA_XMLTYPE});
        $sth->bind_param(":ind" , $i);
        $sth-> execute();


        $i++;
    }
}

我在绑定参数中遇到错误。

【问题讨论】:

    标签: oracle perl error-handling sqlbindparameter


    【解决方案1】:

    错误处理通常通过Try::Tiny 模块完成:

    use Try::Tiny;
    
    try {
        something_that_could_die();
    }
    catch {
        handle_error($_);
    }
    finally {
        do_something_either_way();
    }; # ← trailing semicolon not optional.
    

    catchfinally 都是可选的。

    【讨论】:

    • 这正是我想要的。非常感谢!!
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2017-06-13
    • 2012-06-24
    • 2016-03-06
    • 2022-07-25
    • 2020-10-15
    相关资源
    最近更新 更多