先寫在這裏吧,以後再改進~~http://fanqiang.chinaunix.net/program/perl/2006-05-31/4379.shtml html
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);
$sth->execute() or die "db operation Error:$dbh->errstr"; #執行
my $result=$sth->fetchall_arrayall();
$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 ()抽取方法的返回是一個包含查詢結果的數組。還有其它方法: mysql
fetchrow_arrayref ()返回一個數組引用。 sql
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')"
)
;