Re: perl and postgresql - Mailing list pgsql-novice
From | Ville Jungman |
---|---|
Subject | Re: perl and postgresql |
Date | |
Msg-id | F59miODNFYHpZP5ui4C00029eec@hotmail.com Whole thread Raw |
In response to | perl and postgresql ("Sugrue, Sean" <sean.sugrue@analog.com>) |
Responses |
Re: perl and postgresql
|
List | 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)=@_; 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@shaw.ca >To: "Sugrue, Sean" <sean.sugrue@analog.com> >CC: pgsql-novice@postgresql.org >Subject: Re: [NOVICE] perl and postgresql >Date: Mon, 17 Mar 2003 08:55:11 -0800 > >----- Original Message ----- >From: "Sugrue, Sean" <sean.sugrue@analog.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)=@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@shaw.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
pgsql-novice by date: