先写在这里吧,之后再改进~~
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;
}
- ##------------------------------------------------------------------------------------------------
- ##利用perl DBI创建数据库stucourse,并创建student,course,grade表
- ##------------------------------------------------------------------------------------------------
- use DBI;
- my $db_name = "stucourse"; #数据库名,如果与现有数据库冲突,可改为其他名字
- my $db_host = "localhost"; #主机名
- my $db_port = ‘3306‘; #端口号
- my $username = "root"; #用户名
- my $password = "123"; #密码
- my $dsn = "dbi:mysql:database=${db_name};hostname=${db_host};port=${db_port}";#数据源
-
- #获取驱动程序对象句柄
- my $drh=DBI->install_driver("mysql");
- #如果存在数据库$db_name,则删除之
- if($rc = $drh->func("dropdb",$db_name ,$db_host,$username,$password,"admin") ){
- print "drop database `",$db_name,"` successfully!\n";
- }
- #创建数据库$db_name
- $rc = $drh->func("createdb",$db_name ,$db_host,$username,$password,"admin")or
- die "failed to create database ",$db_name,"!\n";
- print "create database `stucourse` successfully!\n";
-
- #获取数据库句柄
- my $dbh = DBI -> connect ($dsn, $username, $password,{RaiseError => 1, PrintError => 0})or
- die "failed to connect to the database!\n",DBI->errstr();
-
- #设置数据库字符集,防止中文乱码
- my $charset = "set character_set_database=utf8";
- my $sth = $dbh->prepare($charset);
- $sth->execute();
-
- #创建表course
- my $query = "CREATE TABLE `course` ( "
- ."`cid` int(10) NOT NULL auto_increment,"
- ."`cno` varchar(20) NOT NULL, "
- ."`cname` varchar(20) default NULL, "
- ."PRIMARY KEY (`cid`)"
- .")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
- my $sth = $dbh->prepare($query);
- $sth->execute() or die "create table course error: ".$sth->errstr();
- print "create table `course` successfully!\n";
-
- #创建表student
- my $query = "CREATE TABLE `student` ("
- ."`sid` int(10) NOT NULL auto_increment,"
- ."`sno` varchar(20) NOT NULL,"
- ."`sname` varchar(20) default NULL,"
- ."PRIMARY KEY (`sid`)"
- .")ENGINE=InnoDB DEFAULT CHARSET=utf8;";
- my $sth = $dbh->prepare($query);
- $sth->execute() or die "create table student error: ".$sth->errstr();
- print "create table `student` successfully!\n";
-
- #创建表grade
- my $query = "CREATE TABLE `grade` ("
- ."`gid` int(10) NOT NULL,"
- ."`cid` int(10) NOT NULL,"
- ."`sid` int(10) NOT NULL,"
- ."`grade` int(10) default NULL,"
- ."PRIMARY KEY (`gid`),"
- ."KEY `cid` (`cid`),"
- ."KEY `sid` (`sid`),"
- ."CONSTRAINT `cid` FOREIGN KEY (`cid`) REFERENCES `course`(`cid`),"
- ."CONSTRAINT `sid` FOREIGN KEY (`sid`) REFERENCES `student`(`sid`)"
- .") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
- my $sth = $dbh->prepare($query);
- $sth->execute() or die "create table grade error: ".$sth->errstr();
- print "create table `grade` successfully!\n";
-
- #关闭数据库连接
- $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
原文:http://my.oschina.net/u/347414/blog/353937