Linux环境下Perl语言对数据库的操纵

发表于:2007-06-11来源:作者:点击数: 标签:
实例:同一主机下的运行 主机配置:PIII450 128M 15GB 操作系统:Red Hat Linux 6.1 数据库:MySQL-3.22.29 建议使用RPM方式安装,至少要安装下面三个包: MySQL-3.22.29-1.i386.rpm MySQL-client-3.22.29-1.i386.rpm MySQL-devel-3.22.29-1.i386.rpm WWW 服
实例:同一主机下的运行

主机配置:PIII450 128M 15GB

操作系统:Red Hat Linux 6.1

数据库:MySQL-3.22.29

建议使用RPM方式安装,至少要安装下面三个包:

MySQL-3.22.29-1.i386.rpm

MySQL-client-3.22.29-1.i386.rpm

MySQL-devel-3.22.29-1.i386.rpm

WWW服务器:Apache 1.3.6 for Linux

Perl 解释器:version 5.005_03 built for i386-linux

DBI: 版本为:1.13

Data-ShowTable: 版本为:3.3

DBD: Msql-Mysql-modules-1.2018

本人成功地在上述环境下实现了对MySQL数据库的访问。与Windows环境下的数据库不同,它不需要建立外部数据源.

下面是一个应用的简单例子:

为了统计用户访问我们的网站的次数,我们建立了一个数据库, 在其中建一个表,表名为:usedata, 共有三列:userno,userpass,lognum,分别代表用户名,密码,登录次数.

假定我们使用test数据库,在该库中创建表usedata, 我们既可以编一个PERL程序实现,也可以在命令行方式下实现:

<br>

#mysql test<br>

>CREATE TABLE usedata(userno CHAR(8) NOT NULL, usepass CHAR(8) NOT NULL, lognum INT);<br>

>exit<br>

共有两个htm文件,两个pl文件. <br>

regist.htm <html><br>

<head><br>

<meta http-equiv=\"Content-Type\"<br>

content=\"text/html; charset=gb_2312-80\"><br>

<meta name=\"GENERATOR\" content=\"Microsoft FrontPage Express 2.0\"><br>

<title>注册</title><br>

</head><br>

<body><br>

<p><center><font color=ff3380>用户在线注册</font></center><br>

</p><br>

<form action=\"/cgi-bin/database/regist.pl\" method=\"POST\"><br>

<table border=\"0\"> <br>

<td valign=\"top\"><font color=\"#400080\">用户名<input type=text name=\IDNO\ size=4 maxlength=8></font><br>

<td valign=\"top\"><font color=\"#400080\">密码<input type=password name=\PASS\ size=6 maxlength=8></font><br>

</table><br>

<font color=\"#0080C0\"><input type=\"submit\"<br>

value=\"注册\"> <input type=\"reset\" value=\"清除\"> </font><br>

</form><br>

</body><br>

</html> <br>

login.htm <html><br>

<head><br>

<meta http-equiv=\"Content-Type\"<br>

content=\"text/html; charset=gb_2312-80\"><br>

<meta name=\"GENERATOR\" content=\"Microsoft FrontPage Express 2.0\"><br>

<title>登录</title><br>

</head><br>

<body><br>

<p><center><font color=ff3380>用户在线登录</font></center><br>

</p><br>

<form action=\"/cgi-bin/database/login.pl\" method=\"POST\"><br>

<table border=\"0\"> <br>

<td valign=\"top\"><font color=\"#400080\">用户名<input type=text name=\IDNO\ size=4 maxlength=8></font><br>

<td valign=\"top\"><font color=\"#400080\">密码<input type=password name=\PASS\ size=6 maxlength=8></font><br>

</table><br>

<font color=\"#0080C0\"><input type=\"submit\"<br>

value=\"登录\"> <input type=\"reset\" value=\"清除\"> </font><br>

</form><br>

</body><br>

</html> <br>

regist.pl #!/usr/bin/perl<br>

# regist.pl

<p>read(STDIN, $buffer, $ENV{\CONTENT_LENGTH\});<br>

@pairs = split(/&/, $buffer);<br>

foreach $pair (@pairs) {<br>

($name, $value) = split(/=/, $pair);<br>

$value =~ tr/+/ /;<br>

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;<br>

$value =~ s/<!--(.|\\n)*-->//g;<br>

$value=~ s/<([^>]|\\n)*>//g;<br>

$value=~ s/>/>/g;<br>

$value=~ s/</</g; <br>

$FORM{$name} = $value;<br>

}

<p>print \"Content-type: text/html\\n\\n\";

<p>print \"<HTML><HEAD><TITLE>注册</TITLE></HEAD>\\n\";

<p>print \"<body bgcolor=#FFFFFF>\\n\";

<p><br>

if ($FORM{\IDNO\} eq \"\")<br>

{<br>

print<<EOF;<br>

<center><font color=red>用户名不能为空!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

exit(0);<br>

}

<p>if ($FORM{\PASS\} eq \"\")<br>

{<br>

print<<EOF;<br>

<center><font color=red>密码不能为空!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

exit(0);<br>

}

<p>$host= shift || \"\";<br>

$test_db=\"test\";<br>

$opt_user=$opt_password=\"\";

<p>use DBI;

<p>$|= 1; # Autoflush

<p>$table=\"usedata\";

<p>$dbh = DBI->connect(\"DBI:mysql:$test_db:$host\",$opt_user,$opt_password) || die \"Can\t connect: $DBI::errstr\\n\";

<p>$n1 = $FORM{\IDNO\};

<p>$sth=$dbh->prepare(\"select * from usedata where userno=$n1 \") or die $dbh->errstr;

<p>$sth->execute() or die $sth->errstr;

<p>if (($row = $sth->fetchrow_arrayref))<br>

{<br>

$dbh->disconnect();<br>

print<<EOF;<br>

<center><font color=red>用户名已存在!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

exit(0);<br>

}<br>

$n2 = $FORM{\PASS\};<br>

$n3 = 1;<br>

$dbh->do(\"insert into $table values($n1, $n2,$n3)\") or die $DBI::errstr;

<p>$dbh->disconnect();<br>

print<<EOF;<br>

<center>您已注册成功!</center><br>

</BODY></HTML><br>

EOF<br>

exit(0);

<p>login.pl #!/usr/bin/perl<br>

# login.pl

<p>read(STDIN, $buffer, $ENV{\CONTENT_LENGTH\});<br>

@pairs = split(/&/, $buffer);<br>

foreach $pair (@pairs) {<br>

($name, $value) = split(/=/, $pair);<br>

$value =~ tr/+/ /;<br>

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;<br>

$value =~ s/<!--(.|\\n)*-->//g;<br>

$value=~ s/<([^>]|\\n)*>//g;<br>

$value=~ s/>/>/g;<br>

$value=~ s/</</g; <br>

$FORM{$name} = $value;<br>

}

<p>print \"Content-type: text/html\\n\\n\";

<p>print \"<HTML><HEAD><TITLE>登录</TITLE></HEAD>\\n\";

<p>print \"<body bgcolor=#FFFFFF>\\n\";

<p><br>

if ($FORM{\IDNO\} eq \"\")<br>

{<br>

print<<EOF;<br>

<center><font color=red>用户名不能为空!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

exit(0);<br>

}

<p>if ($FORM{\PASS\} eq \"\")<br>

{<br>

print<<EOF;<br>

<center><font color=red>密码不能为空!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

exit(0);<br>

}

<p>$host= shift || \"\";<br>

$test_db=\"test\";<br>

$opt_user=$opt_password=\"\";<br>

use DBI;<br>

$|= 1; # Autoflush

<p>$table=\"usedata\";

<p>$dbh = DBI->connect(\"DBI:mysql:$test_db:$host\",$opt_user,$opt_password) || die \"Can\t connect: $DBI::errstr\\n\";

<p>$n1 = $FORM{\IDNO\};<br>

$sth=$dbh->prepare(\"select * from usedata where userno=$n1 \") or die $dbh->errstr;

<p>$sth->execute() or die $sth->errstr;<br>

if (($row = $sth->fetchrow_arrayref))<br>

{<br>

if ($row->[1] eq $FORM{\PASS\})<br>

{<br>

$NUM = $row->[2]+1;<br>

$sth=$dbh->prepare(\"UPDATE usedata SET lognum=$NUM where userno=$n1 \") or die $dbh->errstr;<br>

$sth->execute() or die $sth->errstr; print<<EOF;<br>

<center>您第 $NUM 次登录!</center><br>

EOF<br>

}<br>

else<br>

{<br>

print<<EOF; <br>

<center><font color=red>密码不正确!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

}<br>

}<br>

else<br>

{<br>

print<<EOF; <br>

<center><font color=red>用户名不正确!</font><br>

返回<a href=\javascript:history.go(-1);\>前页</a>修改<br>

</center><br>

EOF<br>

}<br>

$dbh->disconnect();<br>

print <<EOF;<br>

</body></html><br>

EOF<br>

exit(0); <br>

原文转自:http://www.ltesting.net

...