Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

INSERT INTO POSTGIS TABLE USING PERL

Posted on 2006-06-17
9
Medium Priority
?
946 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

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 Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

596 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