--thanks for the reply.

--i see the same example in the Perldoc for DBI,
--but what i don't see is if i can substitute:


>>> $sth->execute('Bill', 32);

with something like this:

>>> $sth->execute ('select tableowner from t_table');

and have the results from that be put in
place of '?'.

--but perhaps i am getting too far ahead of myself
--(more likely than not).  in the above example,
--you assign 'Bill' and '32' ... but what if 
--i have a text file created (say the list of
--users from the original query).  how can i
--put the list in?  can i say something like

>>> $sth->execute ($file);

--maybe read($file)? this is the part that doesn't
--make sense to me.  how can i pass the list
--of users inside the sql?  there is a part in 
--the perldoc DBI that says something like

open FH, "phone.csv" or die "blah, blah dead:$!";
while (<FH>) {
chomp;
my ($name, phone) = split /,/;
$sth->execute($name, $phone);
;
close $FH;

--i'm going to test this to see if i can
--use my text file to put data into the
--query ... 

--it may just be that i will have to do 
--this in a two or three step process.

--thanks again!

-X

-----Original Message-----
From: Robin Norwood [mailto:[EMAIL PROTECTED]
Sent: Monday, June 16, 2003 4:31 PM
To: Johnson, Shaunn
Cc: beginners_perl (E-mail)
Subject: Re: examples of place holders


"Johnson, Shaunn" <[EMAIL PROTECTED]> writes:

> Howdy:
> 
> Can someone give me an example of using
> place holders with the DBI module?
> 
> I am trying to use the results of one query
> and embed that into another query later.  For
> example: the result of the first query
> (say, select distinct user_name from t_table)
> should go into the second (say select table_owner,
> table_columns, table_def from master_table where
> table_owner = ? ).
> 
> But I don't know exactly *how* the list from the
> first query gets passed down to the place
> holder '?'.
> 
> I'm still working on the script - right now, I
> can do either part independently, but I don't know
> how to pass the results from one to the other.
> I was hoping the place holder feature in the DBI
> module could help.
> 
> Suggestions?  Thanks!

Hrm.  Well, assuming you've looked under 'Placeholders and Bind
Values' in `perldoc DBI`, here's your example, assuming you've already
got a '$dbh':


my $query =<<EOQ;
SELECT table_owner,
       table_columns,
       table_def
  FROM master_table
 WHERE table_owner = ?
   AND table_columns <= ?
EOQ

my $sth = $dbh->prepare($query);
$sth->execute('Bill', 32);

while (my ($owner, $columns, $def) = $sth->fetchrow) {
# ...whatever...
}


So this should get all of the tables owned by 'Bill' with 32 or fewer
columns.  When the query is run, the question marks are replaced by
the params to 'execute', in order.  The technical details are best
left to perldoc DBI - if you haven't read it, do so now...you'll be
glad you did.

-RN

-- 
Robin Norwood
Red Hat, Inc.

"The Sage does nothing, yet nothing remains undone."
-Lao Tzu, Te Tao Ching

Reply via email to