Chinaunix首页 | 论坛 | 博客
  • 博客访问: 628681
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: Python/Ruby

2012-04-22 17:10:53


 

点击(此处)折叠或打开

  1. ##------------------------------------------------------------------------------------------------
  2. ##利用perl DBI创建数据库stucourse,并创建student,course,grade表
  3. ##------------------------------------------------------------------------------------------------
  4. use DBI;
  5. my $db_name = "stucourse"; #数据库名,如果与现有数据库冲突,可改为其他名字
  6. my $db_host = "localhost"; #主机名
  7. my $db_port = '3306'; #端口号
  8. my $username = "root"; #用户名
  9. my $password = "123"; #密码
  10. my $dsn = "dbi:mysql:database=${db_name};hostname=${db_host};port=${db_port}";#数据源

  11. #获取驱动程序对象句柄
  12. my $drh=DBI->install_driver("mysql");
  13. #如果存在数据库$db_name,则删除之
  14. if($rc = $drh->func("dropdb",$db_name ,$db_host,$username,$password,"admin") ){
  15.     print "drop database `",$db_name,"` successfully!\n";
  16. }
  17. #创建数据库$db_name
  18. $rc = $drh->func("createdb",$db_name ,$db_host,$username,$password,"admin")or
  19.     die "failed to create database ",$db_name,"!\n";
  20. print "create database `stucourse` successfully!\n";

  21. #获取数据库句柄
  22. my $dbh = DBI -> connect ($dsn, $username, $password,{RaiseError => 1, PrintError => 0})or
  23.     die "failed to connect to the database!\n",DBI->errstr();

  24. #设置数据库字符集,防止中文乱码
  25. my $charset = "set character_set_database=utf8";
  26. my $sth = $dbh->prepare($charset);
  27. $sth->execute();

  28. #创建表course
  29. my $query = "CREATE TABLE `course` ( "
  30.             ."`cid` int(10) NOT NULL auto_increment,"
  31.             ."`cno` varchar(20) NOT NULL, "
  32.             ."`cname` varchar(20) default NULL, "
  33.             ."PRIMARY KEY (`cid`)"
  34.             .")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  35. my $sth = $dbh->prepare($query);
  36. $sth->execute() or die "create table course error: ".$sth->errstr();
  37. print "create table `course` successfully!\n";

  38. #创建表student
  39. my $query = "CREATE TABLE `student` ("
  40.             ."`sid` int(10) NOT NULL auto_increment,"
  41.             ."`sno` varchar(20) NOT NULL,"
  42.             ."`sname` varchar(20) default NULL,"
  43.             ."PRIMARY KEY (`sid`)"
  44.             .")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  45. my $sth = $dbh->prepare($query);
  46. $sth->execute() or die "create table student error: ".$sth->errstr();
  47. print "create table `student` successfully!\n";

  48. #创建表grade
  49. my $query = "CREATE TABLE `grade` ("
  50.          ."`gid` int(10) NOT NULL,"
  51.          ."`cid` int(10) NOT NULL,"
  52.          ."`sid` int(10) NOT NULL,"
  53.          ."`grade` int(10) default NULL,"
  54.          ."PRIMARY KEY (`gid`),"
  55.          ."KEY `cid` (`cid`),"
  56.          ."KEY `sid` (`sid`),"
  57.          ."CONSTRAINT `cid` FOREIGN KEY (`cid`) REFERENCES `course`(`cid`),"
  58.          ."CONSTRAINT `sid` FOREIGN KEY (`sid`) REFERENCES `student`(`sid`)"
  59.          .") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
  60. my $sth = $dbh->prepare($query);
  61. $sth->execute() or die "create table grade error: ".$sth->errstr();
  62. print "create table `grade` successfully!\n";

  63. #关闭数据库连接
  64. $dbh->disconnect();


 

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