Perl连接DBI

730阅读 0评论2013-03-28 zhengsenlin888
分类:LINUX


  1. #!/usr/bin/perl

  2. #author: zhengsenlin
  3. #date: 2013-03-12
  4. #desc: perl conn mysql

  5. use 5.012;
  6. use strict;
  7. use warnings;
  8. use autodie;

  9. #load module
  10. use DBI;
  11. use Time::Piece;

  12. #mysql config
  13. my $mhost = "localhost";
  14. my $muser = "zheng";
  15. my $mpass = "123456";
  16. my $mport = 3306;
  17. my $mdatabase = "ffff";

  18. #connect mysql
  19. my $dbh = DBI->connect("DBI:mysql:database=$mdatabase;host=$mhost;port=$mport", $muser, $mpass, {'RaiseError' => 1});
  20. my $sth = $dbh->prepare('select * from prowl');
  21. $sth->execute();
  22. while (my $ref = $sth->fetchrow_hashref()) {
  23.     my $t = localtime($ref->{'monit_time'});
  24.     my $real_time = $t->ymd . " " . $t->hms;
  25.     say "$ref->{'server'}\t$ref->{'type'}\t$ref->{'value'}\t$real_time";
  26. }
  27. $sth->finish();

  28. #disconnect mysql
  29. $dbh->disconnect();


上一篇:while exec读取多个变量
下一篇:Perl连接DBI进行update