Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69210
  • 博文数量: 13
  • 博客积分: 247
  • 博客等级: 二等列兵
  • 技术积分: 138
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-02 18:17
文章分类

全部博文(13)

文章存档

2015年(1)

2014年(1)

2013年(1)

2012年(2)

2011年(8)

我的朋友

分类: Python/Ruby

2012-05-11 17:28:33


  1. #! /usr/bin/perl -w
  2. #######mysql database manipulation program using Programming Perl DBI
  3. #######you can modify/redistribute it as perl itself
  4. use strict;
  5. use DBI;
  6. sub get_choice
  7. {
  8.    my $option;
  9.    print STDOUT "Please select a Choice for songwolf database:\n";
  10.    print STDOUT "1==========retrive information of specific id=====================\n";
  11.    print STDOUT "2==========update name of specific id=============================\n";
  12.    print STDOUT "3==========delete record of corresponds to a specific id==========\n";
  13.    print STDOUT "4==========insert a new record====================================\n";
  14.    print STDOUT "others=====exit the program======================================\n";
  15.    chomp($option = <>);
  16.    $option;
  17. }

  18. sub db_manipulate()
  19. {
  20.   my $dbh = shift;
  21.   my $option = shift;
  22.   if($option == 1)
  23.   {
  24.      &do_select($dbh);
  25.   }
  26.   elsif($option == 2)
  27.   {
  28.      &do_update($dbh);
  29.   }
  30.   elsif($option == 3)
  31.   {
  32.      &do_delete($dbh);
  33.   }
  34.   elsif($option == 4)
  35.   {
  36.      &do_insert($dbh);
  37.   }
  38.    else
  39.   {
  40.      print STDOUT "--------exiting from the program---------------\n";
  41.      exit 0;
  42.   }
  43. }

  44. sub do_select()
  45. {
  46.   my $dbh = shift;
  47.   my $id=undef;
  48.   print STDOUT "you've selected the retrive option\n";
  49.   print STDOUT "Please input the test_id of which the information you want to retrive:\n";
  50.   chomp($id = <>);
  51.   my $sth = $dbh->prepare("select test_id id,name name from test where test_id= ?") or die "can't prepare select sql statement:$DBI::errstr\n";
  52.   $sth->bind_param(1,$id);
  53.   $sth->execute() or die "can't execute select statement:$DBI::errstr\n";
  54.   my ($result_id,$result_name) = $sth->fetchrow_array();
  55.   {
  56.       if(defined($result_id))
  57.       {
  58.        print "test_id $result_id is of name $result_name\n";
  59.        $sth->finish();
  60.       }
  61.       else
  62.       {
  63.           print "no test record corresponds to the specified test_id\n";
  64.       }
  65.   }
  66. }

  67. sub do_update()
  68. {
  69.     my $dbh = shift;
  70.     my ($id,$new_name);
  71.     print STDOUT "you've selected the update option\n";
  72.     print STDOUT "please enter the test_id of the corresponding record you want to update\n";
  73.     chomp($id = <>);
  74.     print STDOUT "please enter the new name for the specified test_id:\n";
  75.     chomp($new_name=<>);
  76.     my $sth = $dbh->prepare("update test set name= ? where test_id = ? ") or
  77.      die "can't prepare update statement:$DBI::errstr\n";
  78.     $sth->bind_param(1,$new_name);
  79.     $sth->bind_param(2,$id);
  80.     $sth->execute() or warn "can't execute update statement\n";
  81.     $sth->finish();
  82.     ####my $rows = $dbh->do("update test set name= '".$new_name."' where test_id =".$id");
  83.     ####if($rows < 1 ) {print STDOUT "no rows updated\n";
  84. }

  85. sub do_delete()
  86. {
  87.     my $dbh = shift;
  88.     my $id;
  89.     print STDOUT "you've selected the delete option\n";
  90.     print STDOUT "please enter the test_id of the corresponding record you want to delete\n";
  91.     chomp($id = <>);
  92.     my $rows = $dbh->do("delete from test where test_id = ".$id);
  93.     print STDOUT "deleted $rows rows from database\n";
  94. }

  95. sub do_insert()
  96. {
  97.     my $dbh = shift;
  98.     my ($id,$new_name);
  99.     print STDOUT "you've selected the insert option\n";
  100.     print STDOUT "please enter the test_id of the new record\n";
  101.     chomp($id = <>);
  102.     print STDOUT "please enter the name for the new record\n";
  103.     chomp($new_name=<>);
  104.     my $sql = "insert into test (test_id,name) values (".$id.",'".$new_name."')";
  105.     my $rows = $dbh->do($sql);
  106.     print STDOUT "inserted $rows rows into the database\n";
  107. }

  108. my $dbh = DBI->connect("DBI:mysql:database=songwolf;host=localhost;msql_configfile=/etc/init/mysql.conf;mysql_compression=1","songwolf","2613481") or die "can not connect to database:$DBI::errstr\n";
  109. while(1)
  110. {
  111.     my $choice = &get_choice;
  112.     eval{&db_manipulate($dbh,$choice)};
  113.     print "caught error:$@\n" if $@;
  114. }
  115. $dbh->disconnect() or warn "can't disconnect from database:$DBI::errstr\n";

阅读(1817) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~