Re: postgres insert



In article <4565BA89.6070302@xxxxxxxxxxx>,
tom@xxxxxxxxxxx (Tom Allison) wrote:

I've been using something like this for Oracle for some time
and tried it with Postgresql.

(RaiseError doesn't change the outcome)


sub insert_token {
my $token = shift;
eval{ $sth1->execute($token) };
if ($@) {
return 1 if $@ =~ /duplicate key violates unique constraint/;
die "ERROR: $@\n";
}
return $sth1->rows;
}



I get a STDERR warning printed out everytime this has a duplicate key
violation...

Any idea why eval{} doesn't suppress this?

The usual DBI approach doesn't use eval. The DBD::Pg module suggests
strongly that you test the error code that is returned with $sth1->err
after the execute command, and compare it with the standard codes that
are given at:
http://www.postgresql.org/docs/current/static/errcodes-appendix.html

My version would look like: (not tested - and not sure which err code to
use)

sub insert_token {
my $token = shift;
$sth1->execute($token) or
do{
return 1 if $sth1->err == 23505; # "UNIQUE VIOLATION"
die $sth1->errstr;
};
return $sth1->rows;
}

Boyd
.



Relevant Pages

  • sitemap generator for Perl
    ... I want to run the sitemap generator ... Returns the minimum number of links to traverse from the root URL of ... my $class = shift; ...
    (perl.beginners)
  • Re: How can I create instantiable objects (not classes)?
    ... a child object inherits not only its parent object's ... sub fee { ... my $class = shift; ... For example, an object of type Car might receive a message named "ticket," and since a car does not know what to do with a ticket, it would pass that message to an object of type Driver. ...
    (comp.lang.perl.misc)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Massive failed FTP attempts.
    ... made it a little more generic so that it could monitor any log file. ... To run as a daemon and monitor proftpd, sending lockout notifications to ... sub prune_old_entries ... my $line = shift; ...
    (Security-Basics)