HeadScratcher has asked for the wisdom of the Perl Monks concerning the following question:
Hi I'm hoping someone can help with this?
I'm executing code similar to the code bellow (Not actual but it replicates the problem)
I'm inserting point data into a PostgreSQL/PostGIS database
I spotted this warning and managed to hit the keyboard pause button before it disappeared of the screen.
"WARNING: OGC WKT expected, EWKT provided - use GeomFromEWKT() for this"
The error was caused by badly formatted coordinates that is secondary and will need human intervention to resolve!
My question is how do I trap the warning so I can then do something sensible with the problem record(s).
The database doesn’t return an error through DBI->errstr but returns a non existent lastID
I thought it was Perl warnings but that code doesn’t fire
I’m using version 3.5.1 of DBD::PG
#!C:/strawberry/perl/bin/perl.exe use strict; use warnings; use DBI; use DBD::Pg; my $pwd ="MyPwd"; # my $dbh = DBI->connect('dbi:Pg:dbname=my_geo_table;host=dbhost', 'user', $pwd, { PrintError => 0, HandleError => \&handle_error, } ) or handle_error(DBI->errstr); my $some_text ="hello World"; my $LongDEC = "017.12.270"; my $LatDEC = "44.82115"; my $post_id = 123; my $cmd = "insert into latlong (post_id,some_text,geom) VALUES (" +. $dbh->quote($post_id) . "," . $dbh->quote($some_text) . ", ST_GeomFromText('POINT($LongDEC $LatDEC)', 4326) )"; my $query_handle = $dbh->prepare($cmd); $query_handle->execute(); my $lastID = $dbh->last_insert_id(undef,undef,"latlong",unde +f); if(defined(DBI->errstr)) { ## not getting here my $ErrMsg .= "Error: " . handle_error(DBI->errstr); print $ErrMsg . "\n"; } else { print "Finished with $post_id lastid =[$lastID]\n"; } sub handle_error { my $message = shift; return ("### The Error message from DB is '$message' ###\n"); } ## These subs are for trapping warnings local $SIG{__WARN__} = sub { my $message = shift; print "warning $message\n"; # not getting here logger('warning', $message); }; sub logger { my ($level, $msg) = @_; if (open my $out, '>>', $dir . "\\log.txt") { chomp $msg; print $out "$level - $msg\n"; } } #### print out to cmd WARNING: OGC WKT expected, EWKT provided - use GeomFromEWKT() for thi +s Finished with 123 lastid =[567] 1 records processed 0 error-ed
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Capturing warnings from DBD:PG
by RichardK (Parson) on Nov 15, 2015 at 10:52 UTC | |
|
Re: Capturing warnings from DBD:PG
by kennethk (Abbot) on Nov 15, 2015 at 17:30 UTC | |
by HeadScratcher (Novice) on Nov 26, 2015 at 10:21 UTC | |
|
Re: Capturing warnings from DBD:PG
by Happy-the-monk (Canon) on Nov 15, 2015 at 10:11 UTC | |
|
Re: Capturing warnings from DBD:PG
by Pope-O-Matik (Pilgrim) on Nov 16, 2015 at 14:03 UTC | |
by HeadScratcher (Novice) on Nov 26, 2015 at 10:31 UTC | |
by Pope-O-Matik (Pilgrim) on Nov 29, 2015 at 14:11 UTC |