Perl DBI使用实例

#!/usr/bin/perl

#set module
use DBI;

#scanf the mysqluser
print "请输入mysql帐号:\n";
chomp($mysql_n=<STDIN>);
print "请输入mysql密码:\n";
chomp($mysql_p=<STDIN>);
print "请输入需要连接的数据库:\n";
chomp($mysql_d=<STDIN>);

#connect mysql
my $in=DBI->connect("DBI:mysql:database=$mysql_d;host=localhost","$mysql_n","$mysql_p",{'RaiseError'=>1});
#select databases;
my $select=$in->prepare("select * from people");
$select->execute();

#print the list
while(my $list=$select->fetchrow_hashref()){
  print "$list->{'username'}\t";
  print "$list->{'password'}\t";
  print "$list->{'number'}\t";
  print "$list->{'year'}\t";
}
print "\n";
#close mysql
$in->disconnect();

 

posted @ 2013-02-26 22:08  墨迹哥's  阅读(363)  评论(0编辑  收藏  举报