Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: last_insert_id issue

by ikegami (Patriarch)
on Aug 27, 2014 at 19:13 UTC ( [id://1098796]=note: print w/replies, xml ) Need Help??


in reply to last_insert_id issue

Nothing in your code except print sets $! meaningfully, and then only when it returns false.

See the DBI documentation for information on how errors are reported.

Replies are listed 'Best First'.
Re^2: last_insert_id issue
by sowais (Sexton) on Aug 27, 2014 at 20:25 UTC

    Can you elaborate on what you mean by 'meaningfully'? I am using eval to report any DB errors and because execute and certain statement errors were not being caught by it I am using die and $!. I understand $! is basic, if you can give an example of how the error handling code could be written more meaningfully, that would help a beginner like myself.

      Can you elaborate on what you mean by 'meaningfully'?

      In a way that has meaning, makes sense, conveys information, is useful.

      I understand $! is basic

      Actually, in this situation, it's value is meaningless, makes no sense, conveys no information, useless.

      if you can give an example of how the error handling code could be written more meaningfully

      Sorry, I thought the linked document had this.

      I prefer to pass RaiseError => 1 to connect. Otherwise, if you establish there has been a DBI error, you can get it using DBI->errstr. You can also call errstr on database and statement handles if you want to make sure to get the right error when dealing with multiple handles.

      my $id = $dbh->last_insert_id(); die "Foo: " . $dbh->errstr() if !defined($id);

      Personally, I wouldn't wrap it in an eval block. I'd either enable RaiseError on the $dbh handle and let the module tell me when there was an error, or I'd manually check the return status of each db call and die if needed.

      The advantage of letting the module handle the error checking/handling is that it's less code that I have to write while still retaining a reasonable level of error handling. The advantage of manually checking each call is that I have more control over how I want to deal with the error handling.

      my $dbh = DBI->connect("dbi:ODBC:$DSN", {RaiseError => 1}) or die "DB connection failure\n" . $DBI::errstr;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1098796]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found