设为首页 收藏本站
查看: 1167|回复: 0

[经验分享] Perl使用DBI模块访问数据库

[复制链接]

尚未签到

发表于 2017-5-18 11:48:56 | 显示全部楼层 |阅读模式
需要按照DBI模块,如果访问特定的数据库如MySQL还需要安装特定的模块如DBI::MySQL……

Using Database in Perl with DBI:
<!--[if !supportLists]-->1. <!--[endif]-->参考文档:
Installing DBI and Using MySQL with Perl in Linux: http://home.ubalt.edu/abento/752/dbi/
A Short Guide to DBI:http://www.perl.com/pub/a/1999/10/DBI.html
Perl DBI Examples: http://www.saturn5.com/%7Ejwb/dbi-examples.html
Advanced DBI:http://search.cpan.org/~timb/DBI/DBI.pm
DBI sites: http://dbi.perl.org/
Books:《Programming the Perl DBI》。。。

2. You can see below the basic DBI model. The scripts are written in Perl using standard Perl variables, commands and syntax. The DBI has methods and handles which are database software independent. You program to access, change or query a database using the standard SQL language, combined with the DBI methods and handles. You install as many DBD::type modules as you need to support the different database software you may have, but you Perl script will be the same.
<!--[if gte vml 1]><v:shapetype id="_x0000_t75"coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"filled="f" stroked="f"><v:stroke joinstyle="miter" /><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0" /><v:f eqn="sum @0 1 0" /><v:f eqn="sum 0 0 @1" /><v:f eqn="prod @2 1 2" /><v:f eqn="prod @3 21600 pixelWidth" /><v:f eqn="prod @3 21600 pixelHeight" /><v:f eqn="sum @0 0 1" /><v:f eqn="prod @6 1 2" /><v:f eqn="prod @7 21600 pixelWidth" /><v:f eqn="sum @8 21600 0" /><v:f eqn="prod @7 21600 pixelHeight" /><v:f eqn="sum @10 21600 0" /></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /><o:lock v:ext="edit" aspectratio="t" /></v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" alt="" style='width:306pt;height:169.5pt'><v:imagedata src="file:///C:\DOCUME~1\Hemin\LOCALS~1\Temp\msohtml1\01\clip_image001.gif"o:href="http://home.ubalt.edu/abento/752/dbi/DBImodel.gif" /></v:shape><![endif]--><!--[if !vml]--><!--[endif]--> DSC0000.gif


3. Using MySQL with the Perl DBI Interface:
1) Basic concepts:
Perl uses three levels of handlers to deal with databases:
$drh -- the driver handle = DBD::mysql, which is transparent for you once is installed. You will not need regularly to include $drh in your script.
$dbh -- the database handle = what you will need to create a connection to the database, it is different for each type of database software. It has the following format in MySQL:

my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",$user,$password);

$sth -- the statement handle = children of the $dbh, and will let the various statements be declared. The types of statements will depend on the SQL commands you are using. SELECT uses three statements in order to perform the query and display the results: prepare, execute and fetch (see examples below). INSERT,UPDATE and DELETE do not use fetch, only prepare and execute. See the above on-line references and the book on reserve for more details.
2) SELECT:
3
INSERTUPDATEDELETE ……
4. 使用示例:

1) DBI的MySQL中使用SELECT:
DSC0001.gif #!/usr/bin/perl

usestrict;
useDBI();

#connecttothedatabase
my$dbh=DBI->connect("DBI:mysql:database=broker;host=localhost","broker","broker",{'RaiseError'=>1});

#selectdatafromthetable
my$sth=$dbh->prepare("select*frombike");
$sth->execute();

#showtheselectedresults
while(my$ref=$sth->fetchrow_hashref()){
print"Foundarow:id=$ref->{'id'},university=$ref->{'university'} ";
}
$sth->finish();

#disconnectfromthedatabase.
$dbh->disconnect();


2) 在cgi中,同时使用MySQL,将结果显示到网页中:
#!C:perlinperl-w
usewarnings;
useCGIqw(:standard);
useDBI();


printheader;

#!mustuse'my'todefineavariable
my$now_string=localtime();
print"<b>Hello,CGIusingPerl!</b><br/>It's$now_stringNOW!<br/>";


#connecttothedatabase
my$dbh=DBI->connect("DBI:mysql:database=broker;host=localhost","broker","broker",{'RaiseError'=>1});

#selectdatafromthetable
my$sth=$dbh->prepare("select*frombike");
$sth->execute();

print"<p>ContentfromtheDatabase<br/>";
#showtheselectedresults
while(my$ref=$sth->fetchrow_hashref()){
print"Foundarow:id=$ref->{'id'},university=$ref->{'university'} ";
}
$sth->finish();

#disconnectfromthedatabase.
$dbh->disconnect();


3) DBI的插入INSERT:
#connecttothedatabase
my$dbh=DBI->connect("DBI:mysql:database=broker;host=localhost","brokeruser","brokerpassword",{'RaiseError'=>1});

$insert_statement="insertintopostvalues('','$c','$type','$title','$content','$u','$pubtime')";
my$sth=$dbh->prepare($insert_statement);
if($sth->execute()){
$last_insert_id=$dbh->last_insert_id(undef,undef,undef,undef);
printqq[恭喜您,您的信息发布成功了!ID号为$last_insert_id<br/><ahref="p.html">继续发帖</a>|<ahref="s.pl">查看</a>];

}
else{
printqq[抱歉,信息发布失败,请稍后重试。<br/><ahref="p.html">返回</a>];
}

#disconnectfromthedatabase.
$dbh->disconnect();



关于使用DBI访问数据库更多的用法可以参考:

DBI官方网站: http://dbi.perl.org/
<!--[endif]-->

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378768-1-1.html 上篇帖子: Perl 常见的Server internal error的原因 下篇帖子: 用Eclipse构建Perl应用程序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表