Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Quick DBI Fail Question

by Trihedralguy (Pilgrim)
on Nov 07, 2007 at 23:32 UTC ( [id://649591]=perlquestion: print w/replies, xml ) Need Help??

Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

my $emps = $dbh->fetch_rowarray( "Insert into...." );
I was looking over the DBI manual on cpan, but I can't quite figured it out. Basically what I have is an insert statement that might fail and as a result I need some fail logic...I also want to "die" in the process. I have tried a variety of ways including doing evals on the statement and trying to put a mystery $@ to see if it has values, but I haven't come across an exact answer or solution. Can anyone help me out? I will be happy to explain a bit more if I need to, but I'm sure there is a statement somewhere where if the command has an error, then be able to print (to a web browser) and then die at that line. Thanks in advanced for any help.

Replies are listed 'Best First'.
Re: Quick DBI Fail Question
by jZed (Prior) on Nov 07, 2007 at 23:40 UTC
    > my $emps = $dbh->fetch_rowarray( "Insert into...." );
    There are at least five major errors in that short snippet. Use $dbh->do("INSERT INTO ...");

    The best way to get DBI to produce errors is to set $dbh->{RaiseError} = 1 in your connection. Then any errors will cause your program to die with error messages. If you want to see the messages in a browser, you'll need to use something along the lines of Carp qw(fatalsToBrowser).

    update The errors: 1) The method name is fetchrow_array, not fetch_rowarray 2) It is a statement handle method, not a database handle method 3) It is for fetching data from SELECT statements, not for INSERT 4) it doesn't accept SQL as a parameter. 5) It returns an array (go figure), not a scalar.

Re: Quick DBI Fail Question
by KurtSchwind (Chaplain) on Nov 07, 2007 at 23:42 UTC
    Since you aren't expecting a result set, you should'nt use fetch_rowarray. Use 'do' instead.
    $dbh->do("insert into ....") or die ("Ack! Insert failed!");

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: Quick DBI Fail Question
by Trihedralguy (Pilgrim) on Nov 08, 2007 at 00:23 UTC
    I will have to read up on the difference between do and fetch...The code snippet was just kind of an example I forgot to remove...
    I have one more question then...In the "die" statement, how can I go about making that more or less print html.
    I've tried to do "die " print qq^ 'html with quotes goes here' ^";, But I know that cannot work.
      If you set RaiseError in the connection, it will die and print automatically - you don't need to specifically trap or print them. See Carp for how to make the errors appear in a browser.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-24 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found