- #! /usr/bin/perl -w
- #######mysql database manipulation program using Programming Perl DBI
- #######you can modify/redistribute it as perl itself
- use strict;
- use DBI;
- sub get_choice
- {
- my $option;
- print STDOUT "Please select a Choice for songwolf database:\n";
- print STDOUT "1==========retrive information of specific id=====================\n";
- print STDOUT "2==========update name of specific id=============================\n";
- print STDOUT "3==========delete record of corresponds to a specific id==========\n";
- print STDOUT "4==========insert a new record====================================\n";
- print STDOUT "others=====exit the program======================================\n";
- chomp($option = <>);
- $option;
- }
- sub db_manipulate()
- {
- my $dbh = shift;
- my $option = shift;
- if($option == 1)
- {
- &do_select($dbh);
- }
- elsif($option == 2)
- {
- &do_update($dbh);
- }
- elsif($option == 3)
- {
- &do_delete($dbh);
- }
- elsif($option == 4)
- {
- &do_insert($dbh);
- }
- else
- {
- print STDOUT "--------exiting from the program---------------\n";
- exit 0;
- }
- }
- sub do_select()
- {
- my $dbh = shift;
- my $id=undef;
- print STDOUT "you've selected the retrive option\n";
- print STDOUT "Please input the test_id of which the information you want to retrive:\n";
- chomp($id = <>);
- 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";
- $sth->bind_param(1,$id);
- $sth->execute() or die "can't execute select statement:$DBI::errstr\n";
- my ($result_id,$result_name) = $sth->fetchrow_array();
- {
- if(defined($result_id))
- {
- print "test_id $result_id is of name $result_name\n";
- $sth->finish();
- }
- else
- {
- print "no test record corresponds to the specified test_id\n";
- }
- }
- }
- sub do_update()
- {
- my $dbh = shift;
- my ($id,$new_name);
- print STDOUT "you've selected the update option\n";
- print STDOUT "please enter the test_id of the corresponding record you want to update\n";
- chomp($id = <>);
- print STDOUT "please enter the new name for the specified test_id:\n";
- chomp($new_name=<>);
- my $sth = $dbh->prepare("update test set name= ? where test_id = ? ") or
- die "can't prepare update statement:$DBI::errstr\n";
- $sth->bind_param(1,$new_name);
- $sth->bind_param(2,$id);
- $sth->execute() or warn "can't execute update statement\n";
- $sth->finish();
- ####my $rows = $dbh->do("update test set name= '".$new_name."' where test_id =".$id");
- ####if($rows < 1 ) {print STDOUT "no rows updated\n";
- }
- sub do_delete()
- {
- my $dbh = shift;
- my $id;
- print STDOUT "you've selected the delete option\n";
- print STDOUT "please enter the test_id of the corresponding record you want to delete\n";
- chomp($id = <>);
- my $rows = $dbh->do("delete from test where test_id = ".$id);
- print STDOUT "deleted $rows rows from database\n";
- }
- sub do_insert()
- {
- my $dbh = shift;
- my ($id,$new_name);
- print STDOUT "you've selected the insert option\n";
- print STDOUT "please enter the test_id of the new record\n";
- chomp($id = <>);
- print STDOUT "please enter the name for the new record\n";
- chomp($new_name=<>);
- my $sql = "insert into test (test_id,name) values (".$id.",'".$new_name."')";
- my $rows = $dbh->do($sql);
- print STDOUT "inserted $rows rows into the database\n";
- }
- 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";
- while(1)
- {
- my $choice = &get_choice;
- eval{&db_manipulate($dbh,$choice)};
- print "caught error:$@\n" if $@;
- }
- $dbh->disconnect() or warn "can't disconnect from database:$DBI::errstr\n";
阅读(1829) | 评论(0) | 转发(0) |