Professional Web Applications Themes

How to cate DBI error and pass the control to subroutine? - PERL Beginners

Take a look at the Error module (although you can use eval as well). use DBI; use Error qw(:try); my $dbh = DBI->connect(...); $dbh->{RaiseError} = 1; try { $dbh->do("insert ..."); } catch Error with { print STDERR "Insert failed!"; } $dbh->do("update ..."); ....Or if you only want to update when the insert fails, then put the insert in the catch block. Rob -----Original Message----- From: Titu Kim [mailto:kimtitu] Sent: Wednesday, August 27, 2003 2:37 PM To: [email]beginnersperl.org[/email] Subject: How to cate DBI error and pass the control to subroutine? Hi, I am using DBI and DBD to insert into database. ...

  1. #1

    Default RE: How to cate DBI error and pass the control to subroutine?

    Take a look at the Error module (although you can use eval as well).

    use DBI;
    use Error qw(:try);

    my $dbh = DBI->connect(...);
    $dbh->{RaiseError} = 1;

    try {
    $dbh->do("insert ...");
    }
    catch Error with {
    print STDERR "Insert failed!";
    }

    $dbh->do("update ...");


    ....Or if you only want to update when the insert fails, then put the insert
    in the catch block.

    Rob


    -----Original Message-----
    From: Titu Kim [mailto:kimtitu]
    Sent: Wednesday, August 27, 2003 2:37 PM
    To: [email]beginnersperl.org[/email]
    Subject: How to cate DBI error and pass the control to subroutine?


    Hi,
    I am using DBI and DBD to insert into database. If
    the insert fails because of the key constraints, I
    want to continue to update that record. How can I it
    do this after I issue $sth->execute() statement?


    Thanks.

    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free, easy-to-use web site design software
    [url]http://sitebuilder.[/url]

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    Rob Hanson Guest

  2. #2

    Default RE: How to cate DBI error and pass the control to subroutine?

    Thanks for the reply.
    Thank you for pointing out Error module and try
    and catch block. However, I am trying to avoid
    installing extra module.
    I found an interesting behavior in my script. In a
    subroutine, I perform an insert (RaiseError=>0), then
    i check $dbh->state(Always false if success). If
    $dbh->state is false (Here I assume failure is caused
    by key constraint), then I update the record. The
    interesting part is, if the update statement is within
    the caller subroutine, the record is updated. If i put
    the update statement in another subroutine and have
    the main subroutine to call it, update will not work
    and no error is returned by update. Here is the code
    snippet that will not work and work.

    Working version
    ===============================================
    my $sth=$dbh->prepare(qw {insert_query});
    $sth->execute(bind_param);
    if($sth->state){
    $sth=$dbh->prepare(qw {update_query});
    $sth->execute(bind_param);
    }
    $dbh->commit();
    ================================================

    Bad version
    ================================================
    my $sth=$dbh->prepare(qw {insert_query});
    $sth->execute(bind_param);
    if($sth->state){
    updateSubRoutine($dbh, $param1, $param2);
    }
    $dbh->commit();

    *********updateSubRoutine**********
    sub updateSubRoutine{
    my ($dbh,$param1, $param2) = _;
    my $sth = $dbh->prepare(qw { update_query});
    $sth->execute(params);
    #$dbh->commit(); also don't work.
    }
    ***********************************


    Can someone give me some suggestions?

    Thanks.




    --- "Hanson, Rob" <rhansonaptegrity.com> wrote:
    > Take a look at the Error module (although you can
    > use eval as well).
    >
    > use DBI;
    > use Error qw(:try);
    >
    > my $dbh = DBI->connect(...);
    > $dbh->{RaiseError} = 1;
    >
    > try {
    > $dbh->do("insert ...");
    > }
    > catch Error with {
    > print STDERR "Insert failed!";
    > }
    >
    > $dbh->do("update ...");
    >
    >
    > ...Or if you only want to update when the insert
    > fails, then put the insert
    > in the catch block.
    >
    > Rob
    >
    >
    > -----Original Message-----
    > From: Titu Kim [mailto:kimtitu]
    > Sent: Wednesday, August 27, 2003 2:37 PM
    > To: [email]beginnersperl.org[/email]
    > Subject: How to cate DBI error and pass the control
    > to subroutine?
    >
    >
    > Hi,
    > I am using DBI and DBD to insert into database.
    > If
    > the insert fails because of the key constraints, I
    > want to continue to update that record. How can I it
    > do this after I issue $sth->execute() statement?
    >
    >
    > Thanks.
    >
    > __________________________________
    > Do you Yahoo!?
    > Yahoo! SiteBuilder - Free, easy-to-use web site
    > design software
    > [url]http://sitebuilder.[/url]
    >
    > --
    > To unsubscribe, e-mail:
    > [email]beginners-unsubscribeperl.org[/email]
    > For additional commands, e-mail:
    [email]beginners-helpperl.org[/email]


    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free, easy-to-use web site design software
    [url]http://sitebuilder.[/url]
    Titu Kim Guest

Similar Threads

  1. Pass data from a bind field error
    By EdmondsM in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: June 21st, 05:12 PM
  2. How to pass variables from aspx page to user control
    By Sathyadeva Aithal via .NET 247 in forum ASP.NET Building Controls
    Replies: 1
    Last Post: June 5th, 08:23 PM
  3. IBM CATE Certification
    By rich in forum AIX
    Replies: 0
    Last Post: August 27th, 02:42 PM
  4. "Undefined subroutine" error (but it's defined, I think?)
    By valerian2@hotpop.com in forum PERL Miscellaneous
    Replies: 5
    Last Post: August 12th, 10:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139