Skip site navigation (1) Skip section navigation (2)

Re: perl and postgresql

From: "Ville Jungman" <ville_jungman(at)hotmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: perl and postgresql
Date: 2003-03-17 23:15:30
Message-ID: F59miODNFYHpZP5ui4C00029eec@hotmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
You can run queries with a sub something like this:

   # query postgres
   # $komento == sql-command(s) to run
   # $subs-parameter might be unneeded in some circumstances
   sub kanta{
      my($subs,$komento)=(at)_;
      use Pg;
      my $conn = Pg::connectdb("dbname=YOURDATABASENAME") or die @!;
      my $result=$conn->exec($komento) or die $!;
      my @palaute;
      if(my $em=$conn->errorMessage){
         die "$em: komento: $komento";
      }
      while(my @apu=$result->fetchrow){
         push @palaute,@apu;
      }
      return @palaute;
   }

Using placeholders id faster (I suppose) but this is easier way if you don't 
care about speed so much. Function above returns asked @values if you use 
select-command. I call it like command: $subs->kanta("select * from 
table1");

If someone notices a bug or other stupidities in my routine above, please 
let me know.

ville jungman, ulvilantie 3 b 11, 00350 helsinki, finland
tel. + 358 - 9 - 225 4482 , http://www.kolumbus.fi/vilmak
usko Herraan Jeesukseen, niin sin pelastut. (apt. 16:31)




>From: douggorley(at)shaw(dot)ca
>To: "Sugrue, Sean" <sean(dot)sugrue(at)analog(dot)com>
>CC: pgsql-novice(at)postgresql(dot)org
>Subject: Re: [NOVICE] perl and postgresql
>Date: Mon, 17 Mar 2003 08:55:11 -0800
>
>----- Original Message -----
>From: "Sugrue, Sean" <sean(dot)sugrue(at)analog(dot)com>
>Date: Monday, March 17, 2003 8:39 am
>Subject: [NOVICE] perl and postgresql
>
> >
> >
> > I am trying to execute the following query within perl
> >
> > #!/usr/local/bin/perl
> >
> > use DBI;
> >
> > $prod='stdf';
> >
> >
> > $dbh = DBI-
> > 
> >connect("dbi:Pg:dbname=database;host=mink;port=0000","username","password");
> > $sth = $dbh->prepare("select *  from filestatus where fileformat =
> > $prod");if( defined($sth)){
> >
> > $sth->execute;
> > #for when model numbers are available
> > while (@devices = $sth->fetchrow){
> > ($product,$spec_key)=(at)devices;
> > print"product = $product and speckey = $spec_key \n"; }
> > }
> >
> > i***************************************
> > it works if you put a literal value of 'stdf' for $prod
> > but it fails when I try to use a variable.
> >
> > Another point is if it were an integer the variable would work.
> >
> > Question: How can I get this to work. I've used q// qw// qq// qx//
> >
> > Sean
> >
>
>Try using placeholders.
>
>$prod='stdf';
>$sth = $dbh->prepare("select *  from filestatus where fileformat = ?");
>$sth->execute( $prod );
>
>Doug Gorley | douggorley(at)shaw(dot)ca
>
>
>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 5: Have you checked our extensive FAQ?
>
>http://www.postgresql.org/docs/faqs/FAQ.html


_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail


Responses

pgsql-novice by date

Next:From: Antti HaapalaDate: 2003-03-18 06:25:39
Subject: Re: perl and postgresql
Previous:From: Nabil SayeghDate: 2003-03-17 20:28:24
Subject: Re: 'UNIQUE'

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group