Solved

Perl TK

Posted on 2004-08-16
4
717 Views
Last Modified: 2008-01-09
hi....

i would like to know whether perl tk can be connected to database (mysql)?
if so , can give me some sample codes for searching items from the db?
and i would want the data that is being searched to be display in the perl TK GUI

thankzzz
0
Comment
Question by:TPoly
  • 3
4 Comments
 
LVL 5

Expert Comment

by:ZiaTioN
ID: 11821913
Well the quick answer to your question is yes.

perl/Tk is simply a front end graphical interface used to give an application a nice point and click feel. Anything that perl can do via command line can also be done through Tk as a front end. You can use any of the database modules found at CPAN (http://search.cpan.org/search?query=DBI&mode=all) to accomplish the interaction between your script and your sql database. And yes anything being sent to the database and received from the database can be displayed in the Tk GUI (graphical user interface).

All you would have to do is capture the output from the command sent to the database and redirect it to be printed out to your Tk widget of choice (would have to be a text based widget to have text printed to it, ie. Tk::Text,, Tk::TextUndo, etc..). As far as example code goes for setting up the connection to the database and performaing all other backend functions of the script, there are many tutorials out there that explain how to manipulate any number of perl DBI modules. Here is a link (http://webmonkey.wired.com/webmonkey/98/24/index3a_page3.html?tw=backend) to one that gives a pretty decent explanation. You can go through the entire tutorial to get a better background and understanding on what is going on.
0
 
LVL 5

Accepted Solution

by:
ZiaTioN earned 200 total points
ID: 11822129
Here is a quick example of a Tk gui and an idea on how to redirect the database output to the Tk widget:

--------------------------code----------------------------

#!/usr/bin/perl -w
use Tk;
use DBI;
use strict;

our($filename, $username, $password, $info);

my $db = DBI->connect('DBI:mysql:test:localhost', '','') or die $DBI::errstr;

my $mw = MainWindow->new;
$mw->title("MySQL DataBase Script");

my $f = $mw->Frame->pack(-side => 'top', -fill => 'x');
$f->Label(-text => "Username:")->pack(-side => 'left', -anchor => 'w');
$f->Entry(-textvariable => \$username)->
   pack(-side => 'left', -anchor => 'w', -fill => 'x', -expand => 1);
$f->Label(-text => "Password:")->pack(-side => 'left', -anchor => 'w');
$f->Entry(-textvariable => \$password)->
   pack(-side => 'left', -anchor => 'w', -fill => 'x', -expand => 1);

$f->Button(-text => "Exit", -command => sub { exit; } )->
   pack(-side => 'right');
$f->Button(-text => "Disconnect", -command => \&disconnect)->
   pack(-side => 'right', -anchor => 'e');
$f->Button(-text => "Some Function", -command => \&function)->
   pack(-side => 'right', -anchor => 'e');
$mw->Label(-textvariable => \$info, -relief => 'ridge')->
   pack(-side => 'bottom', -fill => 'x');
my $t = $mw->Scrolled("Text")->pack(-side => 'bottom',
                     -fill => 'both', -expand => 1);

MainLoop;

sub disconnect {
   $db->disconnect();
}

sub function {
   my ($catalog, $schema, $table, $type);
   my $sth = $db->table_info( $catalog, $schema, $table, $type );
   $t->insert("end", $sth);
}

------------------------end code-----------------------------
0
 

Author Comment

by:TPoly
ID: 11827286
eh.. can u explain the codes

thanks
0
 
LVL 5

Expert Comment

by:ZiaTioN
ID: 11831604
Well the majority of it is the GUI. Read up on Tk widgets at CPAN and you will be able to read the code fairly easy. I know at first glance perl/Tk code looks a little jumbled and even overwhelming but it is really fairly easy. Basically "my $mw = MainWindow->new;" just creates the main window and then each widget below that simply builds on this main window. Most the Frame and Label widgets arguments are simply placement or geometric arguments. They tell the application where to place that specific widget with respect to the main window.

"my $db = DBI->connect('DBI:mysql:test:localhost', '','') or die $DBI::errstr;" is just an example of how to create the connection to your database. and then whenever the variable "$db" is used after that it is refering to the connection to the database so $db->disconnect() would disconnect you from your database. At the end of the subroutine "function" you can see the "$t->insert("end", $sth);", this is how you print to a text widget. In this case the text widget is represented by the variable "$t". And you can see we print the value of "$sth" to the text widget. This variable gets it's value from the output or response to the command "$db->table_info( $catalog, $schema, $table, $type );" which would be the info of the specific called table in your database.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

770 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