Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Fetch data from DB and put in to Table Tk

by Cristoforo (Deacon)
on Jul 07, 2012 at 20:40 UTC ( #980511=note: print w/ replies, xml ) Need Help??


in reply to Fetch data from DB and put in to Table Tk

my @name=$sh2->fetchrow_array;
That only fetches the first row from the database query. I think you are expecting 11 rows, (from your code below).
foreach my $j(1..11)
Yet here you are getting $j for 1 to 11 when your sql query only returns 2 items, (code and name). So, if you examined @name immediately after retrieving the query, there should only be index 0 and 1 for @name.

Update: Boy, was a little wrong about the array however. zentara has the solution.

I think the sql section might be like this and then you would have an array @name with contents like zentara's @name.

my $sh2=$db->prepare("select code,name from level "); $sh2->execute() or die(); my @name; while (my $row = $sh2->fetchrow_arrayref) { push @name, $row; }
Also, likely should index $j from 0 .. 10 instead of 1 .. 11

arrays begin with zero, 0, not 1.


Comment on Re: Fetch data from DB and put in to Table Tk
Select or Download Code
Re^2: Fetch data from DB and put in to Table Tk
by gocpon (Novice) on Jul 08, 2012 at 08:15 UTC

    Hi, Thanks for your reply, I changed my code according to you, now my code is running but output is not correct I am getting 'q' for all label in first row of table (TK) and '1' for all label in the second row of the same table (TK). That MySQL table contains data like

    Code Name

    001 Level1

    002 Level2

    003 Level3

    004 Level4

    So I am expecting that first column(code) data of the MySql table should be present in to the first row labels of the Table(Tk) and the same time the second column(Name) data of the MySql table should be present in to the second row labels of the Table(Tk)for your reference i have attached my code also. Please kindly help me.

    #!/usr/bin/perl use Tk; use Tk::Entry; use Tk::Table; use Tk::LabFrame; use warnings; use strict; use DBI; my $v="0.00"; my $mw = MainWindow->new; $mw->geometry("250x400"); $mw->resizable(0,0); $mw->title("Table Example"); my $db=DBI->connect('dbi:mysql:Payroll','root','senthil') or die(); my $sh2=$db->prepare("select code,name from level "); $sh2->execute() or die(); my @name; while (my $row = $sh2->fetchrow_arrayref) { push @name, $row; } my @top2=qw/No Name Amount/; my $table_frame = $mw->LabFrame()->pack(-expand=>1, -fill=>'both'); my $table = $table_frame->Table(-columns => 10, -rows => 6,-fixedrows +=> 1, -scrollbars => 'oe',-relief => 'raised',-background=>'white'); my $j=0; my $i=0; my @ents; foreach my $j(0..10) { my $tmp_label=$table->Label(-text => $name[$i]->[0],-width => 8, -reli +ef =>'raised'); my $tmp_label1 = $table->Label(-text => $name[$i]->[1],-width => 8, -r +elief =>'raised'); my $tmp_label2= $table->Entry( -width => 8, -relief =>'raised',-bg => +'white',-validate=>'key'); $table->put($j,1,$tmp_label); $table->put($j,2,$tmp_label1); $table->put($j,3,$tmp_label2); push @ents, $tmp_label2; $i++; } $table->pack( -expand=>1 , -fill=>'both'); my $button_frame = $mw->Frame( -borderwidth => 4 )->pack(); $button_frame->Button(-text => "Exit", -command => sub {exit})->pack() +; &defineOrder( @ents ); MainLoop; sub defineOrder { my $widget; for (my $i=0; defined( $_[$i+1] ); $i++) { $_[$i]->bind( '<Key-Return>', [\&focus, $_[$i+1]]); $_[$i]->bind( '<Tab>', [\&focus, $_[$i+1]]); } # Uncomment this line if you want to wrap around $_[ $#_ ]->bind('<Key-Return>', [\&focus, $_[0]]); $_[ $#_ ]->bind('<Tab>', [\&focus, $_[0]]); $_[0]->focus; } sub focus { my ($tk, $self) = @_; $self->focus; }
      Please kindly help me.

      Please print out what your @name and $row is, as it comes out of the db. You have a simple dereferencing problem, where what comes out of the db is not exactly what you think. Post it so we can see the result and can help dereferencing. Notice, you are using fetchrow_arrayref, so you might want to look at the values of @$row, instead of $row.

      Another alternative would be to use fetchrow_array instead.

      my @name; my @name1; while (my $row = $sh2->fetchrow_arrayref) { push @name, $row; push @name1, @$row; } # look at the difference print "@name\n"; print "@name1\n";

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
      now my code is running but output is not correct I am getting 'q' for all label in first row of table (TK) and '1' for all label in the second row of the same table

      Yes, I ran it and got the same results as you. I (mistakenly) thought that my $row = $sh2->fetchrow_arrayref provided a fresh instance of $row to push onto the @name array. I confess I'm not really sure what is happening or why. It seems that all entries in the array are the last entry. Perhaps someone else could explain this better.

      To get the correct info, I made the change below.

      #!/usr/bin/perl use strict; use warnings; use DBI; my $dbh = DBI->connect("dbi:SQLite:dbname=users.lite","","", or die "Can't connect"; my $sh2=$dbh->prepare("select code,name from level "); $sh2->execute() or die(); my @name; while (my @row = $sh2->fetchrow_array) { push @name, [@row]; } $dbh->disconnect or die $!; for my $i ( 0 .. $#name) { print "code: $name[$i][0] name: $name[$i][1]\n"; } __END__ *** the program prints code: 001 name: Level1 code: 002 name: Level2 code: 003 name: Level3 code: 004 name: Level4 code: 005 name: Level5 code: 006 name: Level6 code: 007 name: Level7 code: 008 name: Level8 code: 009 name: Level9 code: 0010 name: Level10 code: 0011 name: Level11

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-10-02 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (48 votes), past polls