Re: postgres insert
- From: boyd <tbmoore9@xxxxxxxxxxx>
- Date: Thu, 23 Nov 2006 18:41:52 GMT
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
.
- References:
- postgres insert
- From: Tom Allison
- postgres insert
- Prev by Date: Free Perl Editor
- Next by Date: Re: Free PERL Editor
- Previous by thread: postgres insert
- Next by thread: Re: postgres insert
- Index(es):
Relevant Pages
|