#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::HList;
use Tk::Adjuster;
use DBI;
use encoding 'euc-cn';
my ($login,$server,$username,$password,$database,$db,$sql,$query,$percent_done,@tmp);
my $mw = MainWindow->new(-title=>"Mysql Easy");
$mw->minsize(600,400);
#$mw->resizable(0,0);
#frame
my $menubar=$mw->Frame(-relief=>'groove', -borderwidth=>'2',)
->pack(-side=>'top', -fill=>'x');
my $bottom=$mw->Frame(-relief=>'groove', -borderwidth=>'1',)
->pack(-side=>'bottom', -fill=>'x');
my $left=$mw->Frame(-relief=>'groove', -borderwidth=>'0',)
->pack(-side=>'left', -fill=>'both');
my $aj = $mw->Adjuster(-widget => $left, -side => 'left')
->pack(-side=>'left', -fill=>'y');
my $right=$mw->Frame(-relief=>'groove', -borderwidth=>'1',)
->pack(-side=>'right', -fill=>'both',-expand=>'1');
#menu
my $mb_conn=$menubar->Menubutton(-text=>'Connect')->pack(-side =>'left');
$mb_conn->command(-label=>'Connect', -command=> /&login);
$mb_conn->command(-label=>'Disconnect', -command=> /&saveDBM);
$mb_conn->separator();
$mb_conn->command(-label=>'Exit', -command=> sub{ exit; });
my $mb_view=$menubar->Menubutton(-text=>'View')->pack(-side=>'left');
$mb_view->command(-label=>'Undo', -command=> /&undo);
$mb_view->command(-label=>'Find', -command=> /&findKey);
my $mb_edit=$menubar->Menubutton(-text=>'Edit')->pack(-side=>'left');
$mb_edit->command(-label=>'About DBMedit', -command=> /&about);
my $mb_sqlc=$menubar->Menubutton(-text=>'SQL')->pack(-side=>'left');
$mb_sqlc->command(-label=>'SQL_EXEC', -command=> /&about);
my $mb_help=$menubar->Menubutton(-text=>'Help')->pack(-side=>'left');
$mb_help->command(-label=>'About DBMedit', -command=> /&about);
#HList
my $datalist = $left->Scrolled("HList",-selectmode=>'multiple',-highlightthickness=>0,
-scrollbars=>'e',-height=>8,-header=>1,-background=>'white',-command=>/&showtables)
->pack(-fill=>'x');
$datalist->header('create', 0, -text=> 'database :');
my $tablelist = $left->Scrolled("HList",-selectmode=>'multiple',-highlightthickness=>0,
-scrollbars=>'e',-header=>1,-background=>'white',-command=>/&viewdata)
->pack(-fill=>'both',-expand=>'1');
$tablelist->header('create', 0, -text=> 'table :');
my $list = $right->Scrolled("HList",-selectmode=>'multiple',-highlightthickness=>0,
-scrollbars=>'se',-head=>1,-columns=>7,-background=>'white',-command=>/&showlist)
->pack(-fill=>'both',-expand=>'1');
my @headers = ("Field","Type","Null","Key","Default","Extra"); #标题
foreach my $x(0..6){
$list->header('create',$x,-text=> $headers[$x],);
}
#foot
my $foot = $bottom->Label(-text=>"登陆先...")->pack(-side=>'left');
MainLoop;
sub login {
$login=$mw->Toplevel(-title=> 'Login');
$login->resizable(0,0);
$login->Label(-text=>'Host:')->grid(-row=> 2, -column=> 1, -sticky=> 'w');
$server=$login->Entry()->grid(-row=>2, -column=> 2);
$login->Label(-text=>'Username:')->grid(-row=> 3, -column=> 1, -sticky=> 'w');
$username=$login->Entry()->grid(-row=>3, -column=> 2);
$login->Label(-text=>'Password:')->grid(-row=> 4, -column=> 1, -sticky=> 'w');
$password=$login->Entry(-show=> '*')->grid(-row=>4, -column=> 2);
$login->Label(-text=>'Database:')->grid(-row=> 5, -column=> 1, -sticky=> 'w');
$database=$login->Entry()->grid(-row=>5, -column=> 2);
my $connect=$login->Button(-text=> 'Connect', -command=> /&connect)
->grid(-row=>6, -column=> 1, -columnspan=> 2, -sticky=> 'ew');
$login->Button(-text=> 'Quit', -command=> sub{ $login->destroy; })
->grid(-row=> 7, -column=> 1, -columnspan=> 2, -sticky=> 'ew');
$server->insert(0,"localhost");
$username->insert(0,"root");
$password->insert(0,"123qwe");
$database->insert(0,"mysql");
}
sub connect{
my $hostname = $server->get;
my $username = $username->get;
my $password = $password->get;
my $database = $database->get;
$db = DBI->connect("DBI:mysql:$database",$username,$password);
if ($DBI::errstr){
$login->messageBox(-type=>"OK",-title=>"ERROR",-message=>"$DBI::errstr",-icon=>"error");
}else{
$sql = qq{show databases};
$query = $db->prepare($sql);
$query->execute();
my $result;
while ($result = $query->fetchrow) {
$datalist->add($result, -text=>$result);
}
$query->finish();
$login->destroy;
}
}
sub showtables{
my @tablename = $datalist->selectionGet();
$sql = qq{use $tablename[0]};
$query = $db->prepare($sql);
$query->execute();
$query->finish();
$tablelist->delete('all'); #清除TABLES列表
$sql = qq{show tables};
$query = $db->prepare($sql);
$query->execute();
my $result;
while ($result = $query->fetchrow) {
$tablelist->add($result, -text=>$result);
}
$query->finish();
}
sub viewdata{
$list->delete('all'); #清除LIST列表
@tmp =();
my @viewname = $tablelist->selectionGet();
$sql = qq{show columns from $viewname[0]};
$query = $db->prepare($sql);
$query->execute();
my @result;
while (@result = $query->fetchrow) {
push(@tmp,[@result]);
}
#additem
for my $index (0..$#tmp) {
$list->add($index);
for my $textin (0..scalar(@{$tmp[$index]}-1)) {
$list->itemCreate($index, $textin,-text => $tmp[$index]->[$textin]);
}
}
$query->finish();
}
sub showlist{
my @listindex = $list->selectionGet;
my $listedit=$mw->Toplevel(-title=> 'Edit');
$listedit->resizable(0,0);
$listedit->Label(-text=>'Field :')->grid(-row=> 1, -column=> 1, -sticky=> 'w');
$listedit->Label(-text=>'Type :')->grid(-row=> 1, -column=> 2, -sticky=> 'w');
$listedit->Label(-text=>'Null :')->grid(-row=> 1, -column=> 3, -sticky=> 'w');
$listedit->Label(-text=>'Key :')->grid(-row=> 1, -column=> 4, -sticky=> 'w');
$listedit->Label(-text=>'Default :')->grid(-row=> 1, -column=> 5, -sticky=> 'w');
$listedit->Label(-text=>'Extra :')->grid(-row=> 1, -column=> 6, -sticky=> 'w');
$server=$listedit->Entry(-text=>$tmp[$listindex[0]]->[0])
->grid(-row=>2, -column=> 1);
$server=$listedit->Entry(-text=>$tmp[$listindex[0]]->[1])
->grid(-row=>2, -column=> 2);
$server=$listedit->Entry(-text=>$tmp[$listindex[0]]->[2])
->grid(-row=>2, -column=> 3);
$server=$listedit->Entry(-text=>$tmp[$listindex[0]]->[3])
->grid(-row=>2, -column=> 4);
$server=$listedit->Entry(-text=>$tmp[$listindex[0]]->[4])
->grid(-row=>2, -column=> 5);
$server=$listedit->Entry(-text=>$tmp[$listindex[0]]->[5])
->grid(-row=>2, -column=> 6);
$listedit->Button(-text=>'OK',-command=>sub{ $listedit->destroy; })
->grid(-row=>3,-column=>5,-sticky=> 'ew');
$listedit->Button(-text=>'Cancel',-command=>sub{ $listedit->destroy; })
->grid(-row=>3,-column=>6,-sticky=> 'ew');
}
sub about {
my $about=$mw->messageBox(-type=>'OK', -title=>'About..',
-message=>"Mysql Easy by jondy /n QQ:222411");
}