PerlCookbook2介绍
3/5/2006来源:Perl教程人气:18721
摘要:SQL语言在标准数据库以外的应用
问题:你想对一个非关系型数据库结构进行复杂的SQL查询。
解决:使用CPAN上的DBD::SQLite模块
useDBI;$dbh=DBI->connect("dbi:SQLite:dbname=/Users/gnat/salaries.sqlt","","",{RaiseError=>1,AutoCommit=>1});$dbh->do("UPDATEsalariesSETsalary=2*salaryWHEREname='Nat'");$sth=$dbh->PRepare("SELECTid,deductionsFROMsalariesWHEREname='Nat'");#...
讨论:
SQLite模块定义的“数据库”是存在于单个文件中的,把单个文件仿真为一个数据库。在用useDBI;$dbh=DBI->connect("dbi:SQLite:dbname=/Users/gnat/salaries.sqlt","","",{RaiseError=>1,AutoCommit=>0});eval{$dbh->do("INSERTINTOpeopleVALUES(29,'Nat',1973)");$dbh->do("INSERTINTOpeopleVALUES(30,'William',1999)");$dbh->do("INSERTINTOfather_ofVALUES(29,30)");$dbh->commit();};if($@){eval{$dbh->rollback()};die"Couldn'trollbacktransaction"if$@;}SQLite定义的数据库里面没有数据类型这个概念。不管你在创建一个表的时候指定的是什么数据类型,以后你可以在其中放入任何类型的数值(包括字符型,数字型,日期型,二进制对象/blob)。实际上,创建表的时候你甚至可以不指定数据类型。
CREATETABLEpeople(id,name,birth_year);SQLite只有在要比较数据的时候,如用
CREATETABLEpeople(idINTEGERPRIMARYKEY,name,birth_year);例子14-6说明这一切是怎么工作的
例14-6整形主键#!/usr/bin/perl-w#ipk-demonstrateintegerprimarykeysuseDBI;usestrict;my$dbh=DBI->connect("dbi:SQLite:ipk.dat","","",{RaiseError=>1,AutoCommit=>1});#quietlydropthetableifitalreadyexistedeval{local$dbh->{PrintError}=0;$dbh->do("DROPTABLEnames");};#(re)createit$dbh->do("CREATETABLEnames(idINTEGERPRIMARYKEY,name)");#insertvaluesforeachmy$person(qw(NatTomGuidoLarryDamianJon)){$dbh->do("INSERTINTOnamesVALUES(NULL,'$person')");}#removeamiddlevalue$dbh->do("DELETEFROMnamesWHEREname='Guido'");#addanewvalue$dbh->do("INSERTINTOnamesVALUES(NULL,'Dan')");#displaycontentsofthetablemy$all=$dbh->selectall_arrayref("SELECTid,nameFROMnames");foreachmy$row(@$all){my($id,$Word)=@$row;print"$wordhasid$id\n";}
SQLite支持8位长的字符编码,但是不识别
参照:“Executingan
摘要:发送邮件的时候添加附件
问题:你想要发一封包含附件的邮件,比如包含一份PDF格式的文档
解决:用CPAN上的MIME::Lite模块。
首先,创建包含邮件各种头信息的useMIME::Lite;$msg=MIME::Lite->new(From=>'sender@example.com',To=>'recipient@example.com',Subject=>'Myphotoforthebrochure',Type=>'multipart/mixed');然后用attach方法添加附件内容:
$msg->attach(Type=>'image/jpeg',Path=>'/Users/gnat/photoshopped/nat.jpg',Filename=>'gnat-face.jpg');
$msg->attach(Type=>'TEXT',
Data=>'Ihopeyoucanusethis!');最后,发送这份邮件,发送它的方法是可选的:
$msg->send();#默认的方法是用sendmail规则发送#指定其它的方法$msg->send('smtp','mailserver.example.com');
讨论:
$msg=MIME::Lite->new('X-Song-Playing:'=>'NatchezTrace');
然而,当参数名代表的邮件头在表18-2中时,后面可以不加冒号。下表中*代表通配符,例如Content-*可以代表Content-Type和Content-ID但是不代表Dis-Content
表18-2:
表18-3:
这儿有几个有用的附件编码类型:TEXT代表text/plain,为Type的默认值;BINARY是application/octet-stream的缩写;multipart/mixed表明邮件有附件;application/msword表明附件为微软的Word文档;application/vnd.ms-Excel表明附件为微软的Excel文档;application/pdf表明附件为
#timeoutof30seconds$msg->send("smtp","mail.example.com",Timeout=>30);如果你想创建多个
MIME::Lite->send("smtp","mail.example.com");$msg=MIME::Lite->new(opts);#...$msg->send();#sendsusingSMTP
如果你要处理多个消息,用好ReadNow参数。它指定附件应该立即从文件或文件句柄中读取发送,而不是在发送前转化为字符串。
发送邮件不是
$text=$msg->as_string;print方法可以把消息的字符串形式写入一个文件句柄自定的文件中:
$msg->print($SOME_FILEHANDLE);例子18-3是一个发送邮件的程序,它把在命令行输入的文件名作为附件例18-3:发送带附件的邮件
#!/usr/bin/perl-w#mail-attachment-sendfilesasattachmentsuseMIME::Lite;useGetopt::Std;my$SMTP_SERVER='smtp.example.com';#可根据自己情况改变my$DEFAULT_SENDER='sender@example.com';#同上my$DEFAULT_RECIPIENT='recipient@example.com';#同上MIME::Lite->send('smtp',$SMTP_SERVER,Timeout=>60);my(o,$msg);#processoptionsgetopts('hf:t:s:',\o);$o{f}||=$DEFAULT_SENDER;$o{t}||=$DEFAULT_RECIPIENT;$o{s}||='Yourbinaryfile,sir';if($o{h}or!@ARGV){die"usage:\n\t$0[-h][-ffrom][-tto][-ssubject]file...\n";}#constructandsendemail$msg=newMIME::Lite(From=>$o{f},To=>$o{t},Subject=>$o{s},Data=>"Hi",Type=>"multipart/mixed",);while(@ARGV){$msg->attach('Type'=>'application/octet-stream','Encoding'=>'base64','Path'=>shift@ARGV);}$msg->send();
最新文章推荐