Solved

INSERT INTO POSTGIS TABLE USING PERL

Posted on 2006-06-17
9
925 Views
Last Modified: 2008-01-09
Hello
I am developing a script in Perl that writes points to a spatial table in Postgis. I use :

my $sth = $dbh->prepare("INSERT INTO gtest (geom) VALUES (GeomFromEWKT('SRID=?;POINT(? ? ?)'));")or
     die "Can't prepareSQL: " . $dbh->errstr();;

         $sth->execute($srd, $x, $y, $z)  or  die "Can't  write SQL: " . $sth->errstr();;
When I run the script I get an "Can't write SQL error" that is found near 'SRID='.
And when I use  select  table I get no result.
When I changed the '?' with numbers everything was ok.
Any ideas on how the correct syntax is?
Thank you
George
0
Comment
Question by:geomvigla
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:earth man2
ID: 16930509
I guess you are missing commas, try

my $sth = $dbh->prepare( "INSERT INTO gtest (geom) VALUES ( GeomFromEWKT( SRID=?, POINT( ?, ?, ? )))") or
     die "Can't prepareSQL: " . $dbh->errstr();;

         $sth->execute($srd, $x, $y, $z)  or  die "Can't  write SQL: " . $sth->errstr();;
0
 
LVL 22

Expert Comment

by:earth man2
ID: 16930513
or even ...

my $sth = $dbh->prepare( "INSERT INTO gtest (geom) VALUES ( GeomFromEWKT( ?, POINT( ?, ?, ? )))") or
     die "Can't prepareSQL: " . $dbh->errstr();;
0
 

Author Comment

by:geomvigla
ID: 16935568
Right now my code is:

my $sth = $dbh->prepare(" INSERT INTO gtest (geom) VALUES ( geomFromEWKT(SRID=?,POINT( ? ? ? ))) ")or
     die "Can't prepareSQL: " . $dbh->errstr();;

     $sth->execute($srd,$x,$y,$z)    or die "Can't write sql\n". $sth->errstr();;;

The error I get is
Can't write sql ERROR: syntax error at or near "'35.535295'" at character 79  x=139.38657 y=35.535295 z=18.3

There is no change with or without SRID=

When I have the code with commas I get no point function available.
Any ideas??
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 22

Expert Comment

by:earth man2
ID: 16938156
When I have the code with commas I get no point function available.

-- do you have the point function available to this account ?
0
 
LVL 22

Expert Comment

by:earth man2
ID: 16946777
there are a number of point functions in pg_calalog but I suspect are specifying too many arguments.
 pg_catalog | point | point            | double precision, double precision | postgres | internal | construct_point | convert x, y to point

That is to say point as implented in a standard installation is 2d

try

my $sth = $dbh->prepare( "INSERT INTO gtest (geom) VALUES ( GeomFromEWKT( ?, POINT( ?, ? )))") or
     die "Can't prepareSQL: " . $dbh->errstr();;
0
 

Author Comment

by:geomvigla
ID: 16949637
I use postgis standars and this is a geometry field POINT(X Y Z).
I created through AddGeometryColumn function and the dimension is 3.
0
 

Author Comment

by:geomvigla
ID: 16949695
The sql command that I use directly is:

INSERT INTO gtest (geom) VALUES (GeomFromEWKT('SRID=290;POINT(35.2 27.55 12.5)'));
0
 
LVL 22

Accepted Solution

by:
earth man2 earned 250 total points
ID: 16958948
O
GeomFromEWKT('SRID=290;POINT(35.2 27.55 12.5)'
function has one string argument. So try something like ...

my $sth = $dbh->prepare(" INSERT INTO gtest (geom) VALUES ( geomFromEWKT( ? ) ) ")or
     die "Can't prepareSQL: " . $dbh->errstr();;
   
     $str = 'SRD=' . $srd . ';POINT( ' . $x . ' ' . $y . ' ' . $z . ')';
     $sth->execute($str)    or die "Can't write sql\n". $sth->errstr();;;

0
 

Author Comment

by:geomvigla
ID: 16964302
Finally this is it!
Just a tiny change: it is 'SRID=' so
my $sth = $dbh->prepare(" INSERT INTO gtest (geom) VALUES ( geomFromEWKT( ? ) ) ")or
     die "Can't prepareSQL: " . $dbh->errstr();;
   
     $str = 'SRID=' . $srd . ';POINT( ' . $x . ' ' . $y . ' ' . $z . ')';
     $sth->execute($str)    or die "Can't write sql\n". $sth->errstr();;;

Thank you very much!
George
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question