Professional Web Applications Themes

DBI hashref with multiple db rows - PERL Beginners

I have created a module, and inside one of the Package methods, I have the following code: $href = $getPlanInfo->fetchrow_hashref(); foreach my $key (keys %$href) { print "$key : $href->{$key}\n"; $name = $key; $self->{$name} = $href->{$key}; } Now, in the main program that calls this method, I have the following: my ($user) = new Accounting::EagleUser(); $user->getPlanInfo("steveb"); print "$user->{'plan'} $user->{'username'}\n"; What is happening, is that getPlanInfo() takes a single param, (a username). It then performs a fetchrow_hashref, creating the keys for the user object with the table field names from the db, and the values are the actual data from the ...

  1. #1

    Default DBI hashref with multiple db rows

    I have created a module, and inside one of the Package methods, I have
    the following code:

    $href = $getPlanInfo->fetchrow_hashref();
    foreach my $key (keys %$href) {
    print "$key : $href->{$key}\n";
    $name = $key;
    $self->{$name} = $href->{$key};
    }

    Now, in the main program that calls this method, I have the following:

    my ($user) = new Accounting::EagleUser();
    $user->getPlanInfo("steveb");
    print "$user->{'plan'} $user->{'username'}\n";


    What is happening, is that getPlanInfo() takes a single param, (a
    username). It then performs a fetchrow_hashref, creating the keys for
    the user object with the table field names from the db, and the values
    are the actual data from the table row.

    However, my problem is that some users have more than one row. I have
    tried for days, playing, reading, etc and you guys(gals) feel like my
    last hope. I can't figure out a way to give the user object multiple
    values for a single key. The output when print only shows the fetched
    row that it got first, and it appears the second is never looked at.

    I was thinking that if I implemented something like $self->{$key$i},
    where $i could be an incremented integer, I'd have what I was looking
    for, but how do I iterate through the DB to the next row using
    fetchrow_hashref to do this?

    I really appreciate any insight at all that will help clarify this for
    me, or at least put me back on a path I feel I have wandered waaay off
    of.

    Tks!

    Steve


    Steve Guest

  2. #2

    Default Re: DBI hashref with multiple db rows

    Steve

    You may want to think of this from a DB perspective.

    Is there a unique key associated with each of these lookups that could prevent duplicate rows being returned? If you are looking up info for a particular person, I would assume you would only want 1 returned and you would want a key within the table to ensure this uniqueness.

    HPH
    -Jason 

    Jason Guest

  3. #3

    Default Re: DBI hashref with multiple db rows

    > Steve 

    Thanks Jason,

    I'll have to extract info prior to using the hashref function that
    will return me the unique key for all of the rows, and I'll have to
    mod my queries to return each row seperately, work with it, then call
    the routine again with the next query.

    A little more back and forth to the db than I had hoped, but your
    solution sounds very viable.

    Tks again,

    Steve
     
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >[/ref]


    Steve Guest

  4. #4

    Default Re: DBI hashref with multiple db rows

    Please bottom post....
     
    prevent duplicate rows being returned? If you are looking up info for a
    particular person, I would assume you would only want 1 returned and you
    would want a key within the table to ensure this uniqueness. 

    It is reasonable to have a set of data rows for a particular object.
     [/ref]

    So in the above you want to loop over the result set storing them to the
    object, this sounds like the perfect use of an array reference stored to
    the object. Something like:

    while (my $href = $getPlanInfo->fetchrow_hashref) {
    push {$self->{$name}}, $href;
    }

    Now $self->{$name} contains a list, similar to what you mention with the
    indexes below, but let Perl handle that for you. Then you can loop over
    the list as with any array reference, or index into it similar to any array.

    foreach my $element ({$self->{$name}}) {
    foreach my $key (keys %$element) {
    print "$key: $element->{$name}\n";
    }
    }

    HTH,

    http://danconia.org
     
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >[/ref]


    Wiggins Guest

  5. #5

    Default Re: DBI hashref with multiple db rows

     
    >> have [/ref]
    >
    > So in the above you want to loop over the result set storing them to
    > the
    > object, this sounds like the perfect use of an array reference stored
    > to
    > the object. Something like:
    >
    > while (my $href = $getPlanInfo->fetchrow_hashref) {
    > push {$self->{$name}}, $href;
    > }
    >
    > Now $self->{$name} contains a list, similar to what you mention with
    > the
    > indexes below, but let Perl handle that for you. Then you can loop
    > over
    > the list as with any array reference, or index into it similar to any
    > array.
    >
    > foreach my $element ({$self->{$name}}) {
    > foreach my $key (keys %$element) {
    > print "$key: $element->{$name}\n";
    > }
    > }[/ref]

    Wow! ...and I *almost* understand how it works! Admittedly, I'm just
    begun familiarizing myself with references the last few days, but this
    looks so far like what I need. I am going to research how, and why
    this works, but am I correct with this?:

    (based on your code above, assuming 2 rows in db for user)

    {$self->{$name}}[0] == %hash (name & val) # from db row 1
    {$self->{$name}}[1] == %hash2 (name & val) # from db row 2

    I truly was never aware the true flexibility of Perl before. Wow...if
    I had of researched this stuff before...I don't want to even imagine
    the time I would of saved myself ;o)

    Steve 
    >> following: 
    >> for 
    >> values 
    >> have 
    >> my 
    >> multiple 
    >> fetched 
    >> at. 
    >> $self->{$key$i}, 
    >> looking 
    >> for 
    >> off 
    >>
    >>
    >> --
    >> To unsubscribe, e-mail: org
    >> For additional commands, e-mail: org
    >> <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >>
    >>
    >>[/ref]
    >
    >
    >[/ref]


    Steve Guest

Similar Threads

  1. Multiple rows of data into 1 row
    By Chip Jones in forum Coldfusion Database Access
    Replies: 1
    Last Post: June 9th, 04:25 PM
  2. Inserting Multiple Rows
    By Pappy17 in forum Coldfusion Database Access
    Replies: 2
    Last Post: May 3rd, 06:24 PM
  3. Delete multiple rows
    By Darren Carter in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: January 12th, 10:33 PM
  4. using string as hashref
    By Andrew in forum PERL Beginners
    Replies: 5
    Last Post: April 25th, 07:25 PM
  5. Deleting Multiple Rows
    By Stephen Noronha in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: February 20th, 04:47 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139