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'");#...
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$@;}
CREATETABLEpeople(id,name,birth_year);
CREATETABLEpeople(idINTEGERPRIMARYKEY,name,birth_year);
例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";}
参照:“ExecutinganSQLCommandUsingDBI”CPAN上DBD::SQLite模块的正式文档。它在SQLite的主页上$msg->attach(Type=>'TEXT',Data=>'Ihopeyoucanusethis!'); 最后,发送这份邮件,发送它的方法是可选的:$msg->send();#默认的方法是用sendmail规则发送#指定其它的方法$msg->send('smtp','mailserver.example.com'); 讨论:MIME::Lite模块创建并发送带MIME指定类型附件的邮件。MIME是MultimediaInternetMailExtensions的缩写,而且也是在邮件中附带各种文件文档的标准方式。但是,这个规则并不能从邮件信息中把附件提取出来。如果你想从邮件信息中提取MIME指定类型附件,可以参考这篇文章“ExtractingAttachmentsfromMail”当你创建MIME::Lite对象,以及向创建的对象中添加内容的时候。后面的参数采用“参数名=>值”的有名对形式。有名对的参数名部分应该暗示它代表的邮件头(如,From,To,Subject)以及其它其它MIME::Lite所特有的东西。如果参数名是邮件头,后面应该加上冒号,如:$msg=MIME::Lite->new('X-Song-Playing:'=>'NatchezTrace');然而,当参数名代表的邮件头在表18-2中时,后面可以不加冒号。下表中*代表通配符,例如Content-*可以代表Content-Type和Content-ID但是不代表Dis-Content表18-2:MIME::Lite头ApprovedEncryptedReceivedSenderBccFromReferencesSubjectCcKeywordsReply-ToToCommentsMessage-IDResent-X-Content-*MIME-VersionReturn-PathDateOrganizationMIME::Lite参数类型的完整列表在表18-3中表18-3:MIME::Lite参数类型DataFHReadNowDatestampFilenameTopDispositionIdTypeEncodingLengthFilenamePathMIME::Lite模块的参数类型决定附件的类型和附件的添加方法:Path指定作为附件的文件的路径Filename指定接受方保存附件时,附件的默认文件名。如果指定了Path参数,那么默认的文件名就是路径中的名字Data指定附件添加的日期Type指定待添加附件的文件编码类型Disposition它的值只能是inline和attachment。前者指定接受方打开邮件的时候附件内容会跟在邮件正文后显示,而不单独作为一个附加物。后者指定接受方应该指定一个附件的解码方法,并且保存附件,此时会有提示FH指定一个读取附件的开放的文件句柄这儿有几个有用的附件编码类型:TEXT代表text/plain,为Type的默认值;BINARY是application/octet-stream的缩写;multipart/mixed表明邮件有附件;application/msword表明附件为微软的Word文档;application/vnd.ms-excel表明附件为微软的Excel文档;application/pdf表明附件为PDF文档;image/gif,image/jpeg,image/png分别指定GIF,JPEG,PNG文件;audio/mpeg指定MP3格式文件;video/mpeg指定MPEG格式影片;video/quicktime指定Quicktime格式文件。发送邮件的唯一两种方法是sendmail和Net::SMTP。调用send方法时,若第一个参数为“smtp”,则用Net::SMTP发送邮件。send的其它参数都传给Net::SMTP。#timeoutof30seconds$msg->send("smtp","mail.example.com",Timeout=>30);如果你想创建多个MIME::Lite对象,也就是发送多附件,你可以把send作为类方法调用,此时默认发送方法会被替换。MIME::Lite->send("smtp","mail.example.com");$msg=MIME::Lite->new(opts);#...$msg->send();#sendsusingSMTP如果你要处理多个消息,用好ReadNow参数。它指定附件应该立即从文件或文件句柄中读取发送,而不是在发送前转化为字符串。发送邮件不是MIME::Lite能做的唯一事情。你还可以用它把最后的邮件内容变成字符串:$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();
$msg->attach(Type=>'TEXT',
$msg->send();#默认的方法是用sendmail规则发送#指定其它的方法$msg->send('smtp','mailserver.example.com');
MIME::Lite模块创建并发送带MIME指定类型附件的邮件。MIME是MultimediaInternetMailExtensions的缩写,而且也是在邮件中附带各种文件文档的标准方式。但是,这个规则并不能从邮件信息中把附件提取出来。如果你想从邮件信息中提取MIME指定类型附件,可以参考这篇文章“ExtractingAttachmentsfromMail”
$msg=MIME::Lite->new('X-Song-Playing:'=>'NatchezTrace');
然而,当参数名代表的邮件头在表18-2中时,后面可以不加冒号。下表中*代表通配符,例如Content-*可以代表Content-Type和Content-ID但是不代表Dis-Content
表18-2:MIME::Lite头
MIME::Lite参数类型的完整列表在表18-3中
表18-3:MIME::Lite参数类型
MIME::Lite模块的参数类型决定附件的类型和附件的添加方法:
这儿有几个有用的附件编码类型:TEXT代表text/plain,为Type的默认值;BINARY是application/octet-stream的缩写;multipart/mixed表明邮件有附件;application/msword表明附件为微软的Word文档;application/vnd.ms-excel表明附件为微软的Excel文档;application/pdf表明附件为PDF文档;image/gif,image/jpeg,image/png分别指定GIF,JPEG,PNG文件;audio/mpeg指定MP3格式文件;video/mpeg指定MPEG格式影片;video/quicktime指定Quicktime格式文件。
#timeoutof30seconds$msg->send("smtp","mail.example.com",Timeout=>30);
MIME::Lite->send("smtp","mail.example.com");$msg=MIME::Lite->new(opts);#...$msg->send();#sendsusingSMTP
发送邮件不是MIME::Lite能做的唯一事情。你还可以用它把最后的邮件内容变成字符串:
$text=$msg->as_string;
$msg->print($SOME_FILEHANDLE);
#!/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();