码迷,mamicode.com
首页 > 数据库 > 详细

[perl] 连接mysql

时间:2014-12-08 23:12:03      阅读:540      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

先写在这里吧,之后再改进~~

package mysql_conn;
use DBI;
use strict;
sub new{
 my $class = shift();
 print ("CLASS=$class\n");
 my $self={};
 $self->{"location"} = shift();
 $self->{"db_name"} = shift();
 $self->{"db_user"} = shift();
 $self->{"db_pass"} = shift();
 bless $self,$class;
 return $self;
}
sub get_table_flag_1_3{
 my ($self,$TABLE)=@_;
 my $port = "3306"; #这是mysql的缺省
 my $location=$self->{"location"};
 my $db_name=$self->{"db_name"};
 my $db_user = $self->{"db_user"};
 my $db_pass = $self->{"db_pass"};
 my $database = "DBI:mysql:$dbname:$location:$port";
 my $dbh = DBI->connect($database,$db_user,$db_pass) or die "Cann‘t connect the Database".DBI->errstr;
 my $sql = "SELECT file,owner FROM TABLE where flag=1 or flag=3";
 my $sth = $dbh->prepare($sql);
 my $result=$sth->execute() or die "db operation Error:$dbh->errstr"; #执行
 $sth->finish();
 $dbh->disconnect;#断开数据库连接
 return $result;
}


  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();
http://blog.chinaunix.net/uid-23781137-id-3184595.html


fetchrow_array ()抽取方法的返回是一个包含查询结果的数组。还有其它方法:

fetchrow_arrayref ()返回一个数组引用。

fetchrow_hashref ()返回散列引用。

use  strict ;
use  DBI ;
my   $ dbh   =  DBI - > connect ( "DBI:mysql:database=lybtest;host=localhost" , "root" , "123456" ) ;
my   $ rows   =   $ dbh - > do ( "insert into tbname(id,name,age) values(‘1‘,‘test‘,,‘27‘)" ) ;

[perl] 连接mysql

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://my.oschina.net/u/347414/blog/353937

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!