Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to get individual fields into variables using DBI?

Reply
Thread Tools

How to get individual fields into variables using DBI?

 
 
Fred
Guest
Posts: n/a
 
      02-21-2007
The code fragment below will print all rows in the
table mytable. If there are say 3 fields in mytable,
field1, field2, and field3, how would I get all of
the values into each field? That is, break up
each row by field into variables?

-Thanks



my $dbh = DBI->connect(
"dbig:dbname=$dbname;host=$host;port=$port",
$username, ' '
) or die $!;

my $sth = $dbh->prepare("select * from mytable");

$sth->execute();

while ( @row = $sth->fetchrow_array ) {
print "@row\n";
}

 
Reply With Quote
 
 
 
 
Iain Chalmers
Guest
Posts: n/a
 
      02-21-2007
In article <VuidnUuT3elRVEbYnZ2dnUVZ_t->,
Fred <> wrote:

> The code fragment below will print all rows in the
> table mytable. If there are say 3 fields in mytable,
> field1, field2, and field3, how would I get all of
> the values into each field? That is, break up
> each row by field into variables?


Errmmm, you _do_ know what that "@" symbol in front of @row means, right?

Have you tried something like:

print "col0 = $row[0], col1 = $row[1]\n";

inside that while loop?

big

>
> -Thanks
>
>
>
> my $dbh = DBI->connect(
> "dbig:dbname=$dbname;host=$host;port=$port",
> $username, ' '
> ) or die $!;
>
> my $sth = $dbh->prepare("select * from mytable");
>
> $sth->execute();
>
> while ( @row = $sth->fetchrow_array ) {
> print "@row\n";
> }


--
"Everything you love, everything meaningful with depth and history,
all passionate authentic experiences will be appropriated, mishandled,
watered down, cheapened, repackaged, marketed and sold to the people
you hate." Mr Jalopy quoting Hooptyrides (on jalopyjunktown.com)
 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      02-22-2007
On Wed, 21 Feb 2007 18:09:53 +1100, Iain Chalmers wrote:

> In article <VuidnUuT3elRVEbYnZ2dnUVZ_t->,
> Fred <> wrote:
>
>> The code fragment below will print all rows in the
>> table mytable. If there are say 3 fields in mytable,
>> field1, field2, and field3, how would I get all of
>> the values into each field? That is, break up
>> each row by field into variables?

>
> Errmmm, you _do_ know what that "@" symbol in front of @row means, right?
>
> Have you tried something like:
>
> print "col0 = $row[0], col1 = $row[1]\n";
>
> inside that while loop?
>
> big



Thanks. I was thinking that each element of @row was an
entire row like:

$row[0] = "col1_here col2_here col3_here"
$row[1] = "col1_here col2_here col3_here"

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      02-22-2007
On Thu, 22 Feb 2007 08:40:58 -0500 Fred <> wrote:

F> On Wed, 21 Feb 2007 18:09:53 +1100, Iain Chalmers wrote:
>> In article <VuidnUuT3elRVEbYnZ2dnUVZ_t->,
>> Fred <> wrote:
>>
>>> The code fragment below will print all rows in the
>>> table mytable. If there are say 3 fields in mytable,
>>> field1, field2, and field3, how would I get all of
>>> the values into each field? That is, break up
>>> each row by field into variables?

>>
>> Errmmm, you _do_ know what that "@" symbol in front of @row means, right?
>>
>> Have you tried something like:
>>
>> print "col0 = $row[0], col1 = $row[1]\n";
>>
>> inside that while loop?
>>
>> big


F> Thanks. I was thinking that each element of @row was an
F> entire row like:

F> $row[0] = "col1_here col2_here col3_here"
F> $row[1] = "col1_here col2_here col3_here"

If you are just getting started with DBI, I would suggest looking at
Rose:B::Object. The Loader module will automatically generate all
the code for you (MySQL, SQLite, Postgres, and I'm working on Oracle), so
you can just say

my @results = # your query here

foreach my $item (@results)
{
printf "F1 %s F2 %s F3 %s\n", $item->col1(), $item->col2(), $item->col3();
}

Install Rose:B::Object from CPAN:

perl -MCPAN -eshell'install Rose:B::Object'

and then look at the simple docs for setting up Rose:B and
Rose:B::Object::Loader to write your code for you.

Even if you can't use the Loader to write the code automatically, you
can still use RDBO, just set up the tables yourself, but it's
ridiculously easy to do it with Loader when it's available.

I'm not the author of RDBO, but I like it a *lot*.

Ted
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Access individual fields in csv using python Krishna Python 1 08-07-2008 04:40 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
How do I parse a string into individual characters? (really simple!) really! Jeannie C++ 15 08-30-2005 08:34 AM
Problem accessing individual recordset fields in a Sql Server 7 view via ASP Darren Smith ASP General 2 01-28-2004 08:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57