How to get individual fields into variables using DBI?

F

Fred

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(
"dbi:pg: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";
}
 
I

Iain Chalmers

Fred said:
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

Fred

Fred said:
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"
 
T

Ted Zlatanov

Fred said:
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::DB::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::DB::Object from CPAN:

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

and then look at the simple docs for setting up Rose::DB and
Rose::DB::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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top