?
Solved

INSERT INTO POSTGIS TABLE USING PERL

Posted on 2006-06-17
9
Medium Priority
?
938 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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 this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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