Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to store the data fetched from the database in perl

by frozenwithjoy (Curate)
on Jun 11, 2013 at 06:18 UTC ( #1038195=note: print w/ replies, xml ) Need Help??


in reply to How to store the data fetched from the database in perl

#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use Data::Printer; # populating your hash my %data = map { chomp; $_ => 1 } <DATA>; # for your example, might look something like this: # my %data = map { $_ => 1 } $sth->fetchrow(); # do you mean fetchrow_ +array? # two ways to check whether key exists in hash say "ABC exists" if exists $data{'ABC'}; say "JKL exists" if $data{'JKL'} == 1; p %data; __DATA__ ABC JKL HGJ KJI GHJ LKJ

Output:

{ ABC 1, GHJ 1, HGJ 1, JKL 1, KJI 1, LKJ 1 } ABC exists JKL exists


Comment on Re: How to store the data fetched from the database in perl
Select or Download Code
Re^2: How to store the data fetched from the database in perl
by rajsai28 (Novice) on Jun 11, 2013 at 06:38 UTC

    I have used below code while fetching data from the sql but i got the output as below

    $sth->execute ($lBOA_BLC_MASTER_ID) or die "Cannot execute: " . $sth-> +errstr(); %data = map { $_ => 1 } $sth->fetchrow();

    Output

    $VAR1 = 'ABC'; $VAR2 = 1;
    thanks and regards,
      fetchrow fetches one row at a time. If you want to fetch all the rows, use fetchall_arrayref. See DBI.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I haven't used this module before, so am not totally familiar with this method, but does this work?

      my %data; while ( my $result = $sth->fetchrow() ) { $data{$result} = 1; }
      Try using my $hr_data = $dbh->selectall_hashref('SELECT DISTINCT EMP_NAME FROM EMPLOYEE', [ 'EMP_NAME' ]);
      This fetches all data in one go.
      You can then use say "ABC exists" if exists $hr_data->{ABC}; to check if an element is there.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1038195]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-11-28 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (191 votes), past polls