免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 879 | 回复: 0
打印 上一主题 下一主题

perl连接mysql数据库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 09:46 |只看该作者 |倒序浏览
  1. #!/usr/bin/perl -w

  2. #The script is used to connect mysql and show table members of the
  3. #database library
  4. use DBI();
  5. $datetime=$ARGV[0];
  6. $event=$ARGV[1];
  7. $path=$ARGV[2];
  8. #connect
  9. my $dbh = DBI->connect("DBI:mysql:database=log;host=localhost","root","ceagle",
  10.         {'RaiseError'=>1,'AutoCommit'=>0});

  11. #execute query
  12. #my $sth = $dbh->prepare("SELECT * from members");
  13. #$sth->execute();
  14. eval
  15. {
  16.     #insert data to members
  17.         $dbh->do("insert into loginfo values (NULL,'$datetime','$event','$path')");
  18.         $dbh->commit();

  19. };
  20. #iterate through resultset
  21. my $sth = $dbh->prepare("SELECT * from loginfo");

  22. $sth->execute();
  23. print "id\t\tdatetime\tevent\tpath","\n";
  24. while (my $ref = $sth->fetchrow_hashref()) {
  25.   print "$ref->{'logid'}\t\t$ref->{'datetime'}\t$ref->{'event'}\t$ref->{'path'}\n";
  26. }

  27. #Delete the data
  28. #eval
  29. #{
  30. #    $dbh->do("delete from members where fname = 'cao' ");
  31. #    $dbh->commit();
  32. #
  33. #};
  34. if ($@){
  35.     print "Transaction aborted:$@";
  36.     $dbh->rollback();
  37. }
  38. #clean up
  39. $sth->finish();
  40. $dbh->disconnect();
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP