pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: [SQL] perl and postgres. . .
4+ messages / 4 participants
[nested] [flat]

* Re: [SQL] perl and postgres. . .
@ 1999-04-21 15:02  Gene Selkov Jr. <selkovjr@xnet.com>
  parent: JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Gene Selkov Jr. @ 1999-04-21 15:02 UTC (permalink / raw)
  To: JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>; pgsql-interfaces@hub.org

> I'm trying in perl to get a maximum value from a field in a postgres table:
> $query="select max(userseq) from dataentry;";
> $result=$conn->exec("$query");
> 
> and then capture that value in a scalar variable:
> 	$userseq=($result);
> 
> but it's not working.

$result->getvalue(0, 0)

(pse. read examples in `perldoc Pg`)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* perl and postgres. . .
@ 1999-04-21 15:45  JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
  0 siblings, 2 replies; 4+ messages in thread

From: JT Kirkpatrick @ 1999-04-21 15:45 UTC (permalink / raw)
  To: 'pgsql-interfaces@hub.org' <pgsql-interfaces@hub.org>; 'pgsql-sql@hub.org' <pgsql-sql@hub.org>; 'chugalug@listserv.highertech.net' <chugalug@listserv.highertech.net>

I'm trying in perl to get a maximum value from a field in a postgres table:
$query="select max(userseq) from dataentry;";
$result=$conn->exec("$query");

and then capture that value in a scalar variable:
	$userseq=($result);

but it's not working.  The field userseq is int4 if that matters to you. 
 if I can capture that value in a variable then I can use it in an update 
query for that particular record.  Can anyone enlighten me??

jt




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] perl and postgres. . .
@ 1999-04-21 19:10  Brett W. McCoy <bmccoy@lan2wan.com>
  parent: JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Brett W. McCoy @ 1999-04-21 19:10 UTC (permalink / raw)
  To: JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>; +Cc: 'pgsql-interfaces@hub.org' <pgsql-interfaces@hub.org>; 'pgsql-sql@hub.org' <pgsql-sql@hub.org>; 'chugalug@listserv.highertech.net' <chugalug@listserv.highertech.net>

On Wed, 21 Apr 1999, JT Kirkpatrick wrote:

> I'm trying in perl to get a maximum value from a field in a postgres table:
> $query="select max(userseq) from dataentry;";
> $result=$conn->exec("$query");
> 
> and then capture that value in a scalar variable:
> 	$userseq=($result);
> 
> but it's not working.  The field userseq is int4 if that matters to you. 
>  if I can capture that value in a variable then I can use it in an update 
> query for that particular record.  Can anyone enlighten me??

You can't get row information from a query that way.  You need to use 
$result->fetchrow to get the actual data out of your query (it returns 
Null if there are no more rows to fetch from). You could do something like:

@q_row = $result->fetchrow;
$userseq = $q_row[0];

To get that information.


Brett W. McCoy           
                                         http://www.lan2wan.com/~bmccoy
-----------------------------------------------------------------------
Self Test for Paranoia:
	You know you have it when you can't think of anything that's
your own fault.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] perl and postgres. . .
@ 1999-04-21 19:36  Gene Selkov, Jr. <selkovjr@mcs.anl.gov>
  0 siblings, 0 replies; 4+ messages in thread

From: Gene Selkov, Jr. @ 1999-04-21 19:36 UTC (permalink / raw)
  To: JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>; pgsql-interfaces@hub.org

> I am trying 
> $query="select max((userseq) from dataentry;";
> $result=$conn->exec("$query");
> $userseq=($result->getvalue(0,0));
> 
> * I need the value in a variable name other than $result. . .
> 
> If I try
> $query="select max((userseq) from dataentry;";
> $result->getvalue(0,0);
> $userseq=$result;
> 
> it bombs.  Ahhh, the frustrations of being new to this!  

Now I see what your problem is. You missed the whole point. $result is
not a variable, it is an object reference, so is $conn. The exec()
method of $conn (or rather, that of the object referenced by $conn)
returns the result object. You normally access the object's data
through its methods, in this case, getvalue(), fetchrow(), etc. -- see
the Pg doc for the full list.

So the following simply copies the reference to the result object into
$userseq, and that may bomb, depending on how you use it further:

> $userseq=$result;

The following the value you need, but it since there is no explicit
assignment, it is getting lost:

> $result->getvalue(0,0);

What you needed was

$userseq = $result->getvalue(0,0);

And again, the manual for Pg has complete examples. Note the code you
need to add to check for errors that may result from each method call.


--Gene



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~1999-04-21 19:36 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-04-21 15:45 perl and postgres. . . JT Kirkpatrick <jt-kirkpatrick@mpsllc.com>
1999-04-21 15:02 ` Gene Selkov Jr. <selkovjr@xnet.com>
1999-04-21 19:10 ` Brett W. McCoy <bmccoy@lan2wan.com>
1999-04-21 19:36 Re: [SQL] perl and postgres. . . Gene Selkov, Jr. <selkovjr@mcs.anl.gov>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox