摘要: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"Couldntrollbacktransaction"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:




