Professional Web Applications Themes

DBI: exit status, sql operation - PERL Modules

Gunnar Hjalmarsson <noreplygunnar.cc> wrote in message news:<bpqlpn$1rc0f7$1ID-184292.news.uni-berlin.de>... > Christian Eriksson wrote: > > Gunnar Hjalmarsson wrote: > >> Christian Eriksson wrote: > >>> How to find out if your sql operation was successful or failed > >>> when using Perl Module DBI? > >> > >> One way: > >> > >> $sth->execute or die $sth->errstr; > > > > But what would the exit value be from the perl process in the "die" > > case? What values does $sth->execute return from successful > > operations and failed operations respectively? > > [url]http://search.cpan.org/~timb/DBI-1.38/DBI.pm#execute[/url] From the url you pointed at: ...

  1. #1

    Default Re: DBI: exit status, sql operation

    Gunnar Hjalmarsson <noreplygunnar.cc> wrote in message news:<bpqlpn$1rc0f7$1ID-184292.news.uni-berlin.de>...
    > Christian Eriksson wrote:
    > > Gunnar Hjalmarsson wrote:
    > >> Christian Eriksson wrote:
    > >>> How to find out if your sql operation was successful or failed
    > >>> when using Perl Module DBI?
    > >>
    > >> One way:
    > >>
    > >> $sth->execute or die $sth->errstr;
    > >
    > > But what would the exit value be from the perl process in the "die"
    > > case? What values does $sth->execute return from successful
    > > operations and failed operations respectively?
    >
    > [url]http://search.cpan.org/~timb/DBI-1.38/DBI.pm#execute[/url]
    From the url you pointed at:

    ....................
    $rv = $sth->execute or die $sth->errstr;
    $rv = $sth->execute(bind_values) or die $sth->errstr;

    Perform whatever processing is necessary to execute the prepared
    statement. An undef is returned if an error occurs. A successful
    execute always returns true regardless of the number of rows affected,
    even if it's zero (see below). It is always important to check the
    return status of execute (and most other DBI methods) for errors if
    you're not using "RaiseError".

    For a non-SELECT statement, execute returns the number of rows
    affected, if known. If no rows were affected, then execute returns
    "0E0", which Perl will treat as 0 but will regard as true. Note that
    it is not an error for no rows to be affected by a statement. If the
    number of rows affected is not known, then execute returns -1.
    .....................

    I have some comments/questions here:

    1. "It is always important to check the returns status of execute
    .....". But then I think you can't have "or die $sth->errstr" after the
    "$rv = $sth->execute", right?

    2. In my code below I have removed "or die $sth->errstr" because I
    want to examine the $sth->err to check for certain return codes and
    exit from the perl script accordingly.

    $sth->execute;
    $rc = $sth->err;

    if ($rc < 0 )
    {
    if ($rc = -239)
    {
    exit 239;
    }
    else
    {
    exit 3;
    }
    }

    $sth->finish;

    $dbh->disconnect;

    exit 0;

    Could I be missing errors with this code, i e could this perl script
    exit with 0 without my sql operation (insert of one row in this case)
    been carried out as intended?

    Any ideas of change in the code? I want to catch a duplicate unique
    key with $rc = -239 and then exit with 239 but for all other errors
    exit with 3. In my testing, though I'm not 100 % sure, I think I have
    experienced my script exiting with 0 and then found the insert not
    been done. That's why I believe I must improve/change the code above.

    Regards Christian Eriksson
    Christian Eriksson Guest

  2. #2

    Default Re: DBI: exit status, sql operation

    Christian Eriksson wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> [url]http://search.cpan.org/~timb/DBI-1.38/DBI.pm#execute[/url]
    >
    > I have some comments/questions here:
    >
    > 1. "It is always important to check the returns status of execute
    > ....". But then I think you can't have "or die $sth->errstr" after
    > the "$rv = $sth->execute", right?
    Are you trying to make me agree that the DBI POD is incorrect? :)
    I don't agree on that.

    What's typically important is to have Perl capture the reason why
    something fails, and "$sth->errstr" gives you the reason in cleartext.
    If you prefer the error code, you can of course have
    "or die $sth->err" instead.
    > 2. In my code below I have removed "or die $sth->errstr" because I
    > want to examine the $sth->err to check for certain return codes
    > and exit from the perl script accordingly.
    That code confuses me in a couple of respects.
    > $sth->execute;
    > $rc = $sth->err;
    >
    > if ($rc < 0 )
    I don't know whether that captures all errors. The DBI docs says about
    the 'err' method: "Returns the native database engine error code from
    the last driver method called. The code is typically an integer but
    you should not assume that."
    > {
    > if ($rc = -239)
    That does not do what you think it does. It _assigns_ -239 to $rc, and
    returns always a true value. You probably intended to write:

    if ($rc == -239)

    See "perldoc perlop".

    <snip>
    > Could I be missing errors with this code, i e could this perl
    > script exit with 0 without my sql operation (insert of one row in
    > this case) been carried out as intended?
    As I said, I don't know.
    > Any ideas of change in the code? I want to catch a duplicate unique
    > key with $rc = -239 and then exit with 239 but for all other
    > errors exit with 3.
    How about:

    $sth->execute or myexit($sth->err);
    $sth->finish;
    $dbh->disconnect;
    exit 0;

    sub myexit {
    my $rv = shift;
    exit ($rv == -239 ? 239 : 3);
    }

    That relies on the doented behaviour of the 'execute' method, and
    calls the myexit() function when $sth->execute returns undef.

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

Similar Threads

  1. Net::telnet - need to retrieve the exit status
    By Anita in forum PERL Modules
    Replies: 1
    Last Post: September 2nd, 05:13 AM
  2. ksh script, exit status and pipe
    By Ian Wilson in forum Linux / Unix Administration
    Replies: 1
    Last Post: August 25th, 12:49 PM
  3. Script Exit Status
    By Paul Kraus in forum Perl / CGI
    Replies: 2
    Last Post: December 2nd, 05:36 PM
  4. asking for your co-operation
    By attorrney attorney in forum Ruby
    Replies: 0
    Last Post: August 7th, 05:34 AM
  5. Windows XP operation
    By John Peifer in forum Windows XP/2000/ME
    Replies: 3
    Last Post: July 19th, 03:04 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