Solved

Retrieving data from a database

Posted on 2001-06-27
4
147 Views
Last Modified: 2010-03-05
Hi,

So far I use the code described below to retrieve the data from a database :
   $base->get_result(\%result);
   
   foreach $row (@{$result{data}}) {
      $ligne = "$row->[0],$row->[1],$row->[2],$row->[3],$row->[4],";
      $ligne .= "$row->[5],$row->[6],$row->[7],$row->[8],$row->[9],";
      $ligne .= "$row->[10],$row->[11],$row->[12],$row->[13],$row->[14],";
      $ligne .= "$row->[15],$row->[16],$row->[17],$row->[18],$row->[19],";
      $ligne .= "$row->[20]";
   ...

But now I want to be more flexible and I want to make a loop such as :
   foreach $token ($row) {
    ...

Therefore I don't have to manage explicitly the number of columns of the query.

Thanks in advance
0
Comment
Question by:Richard Coffre
  • 2
4 Comments
 

Expert Comment

by:swonge
ID: 6232009
Hi,
   try following code:
-----------------------------------------------------------
use DBI;
use DBI qw(:sql_types);

sub DBOp {
     my ($dbConn, $query) = (@_);
     $rst = $dbConn->prepare("$query") || &DBErrorRewind($ERRORCODE{"ErrorSQLPrepare"},"getWarning\@parseWarnings.pl",$dbh->errstr(), "$query", $fileName, $Error);    
     $rst->execute() || &DBErrorRewind($ERRORCODE{"ErrorSQLExec"},"getWarning\@parseWarnings.pl",$rst->errstr(), $query, $fileName, $Error);
     return $rst;
}

$query = "select * from db_table";
$rst1 = DBOp($dbConn, $query);

while (@data = $rst1->fetchrow_array()) {
   $val1 = $data[0];
   $val2 = $data[1];
   $val3 = $data[2];
   
   print "val1: $val1 \t val2: $val2 \t val3: $val3\n";
}

----------------------------------------------------------
Hope this help =)


0
 
LVL 3

Accepted Solution

by:
interiot earned 50 total points
ID: 6232030
 $base->get_result(\%result);
 
  foreach $row (@{$result{data}}) {
     my $ligne = '';
     foreach $token (@$row) {
        $ligne .= ", $token";
     }
     print $ligne;
  }


Or, perhaps more succinctly:

  $base->get_result(\%result);

  foreach $row (@{$result{data}}) {
     my $ligne = join(', ', @$row);
     print $ligne;
  }
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6234019
The answer didn't follow the programming methodology I use.
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 6234022
It's exactly what I want easy, simple, short ...

Thanks a lot
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question about @INC variable in perl 1 58
ppm conversion to curl on a module install 8 82
Excel to CSV conversion with specific columns 5 82
Regular Expression for URL 10 97
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question