Perl: 向mysql数据库插入二进制字段

Perl: 向mysql数据库插入二进制字段, 并返回所插入记录的自动增长字段的值
2007-10-27 13:47

#! c:/perl/bin/perl.exe
use strict;
use warnings;
use Digest::MD5;
use DBI;
use DBD::mysql;

my $filePath = "D://malware//ixigua.exe";
my $fileLen = -s $filePath;
my $md5 = Digest::MD5->new();
open ( HFILE, "< $filePath" ) or die ("Can't open:$!");
binmode( HFILE );
$md5->addfile( *HFILE );
my $strMD5 = $md5->hexdigest(); #计算MD5值

seek( HFILE, 0, 0);  #调整文件指针至文件头,必需步骤
my $data;             #存放文件内容
my $len = read( HFILE, $data, $fileLen );
close( HFILE );

my $dbh = DBI -> connect("DBI:mysql:DB_AAA;host=192.168.22.252;user=ixigua;password=aaa123",{RaiseError=>1});
my $insertStr = "insert into `SIMPLE_BODY` ( SIMPLE_SIZE, SIMPLE_BODY) values( ?,?);"; #预留参数的位置
my $sth = $dbh->prepare( $insertStr ) or die("Cannot prepare statement:", $dbh->errstr(),"/n");
my $rc = $sth->execute($fileLen, $data) or die("Cannot execute statement:", $sth->errstr(), "/n");
my $id = $dbh->last_insert_id(undef,undef, "`SIMPLE_BODY`", "SIMPLE_BODY_KEY");
#前2个参数不用理会,第3个为数据表名,第4个具有自动增长属性的列名

print 'insert_id=',$id, "/n";

warn( $DBI::errstr ) if $DBI::err;
$dbh->disconnect();

================================================
注释: 若数据库插入语句不用参数预留, 操作就失败,原因没有找到?
my $insertStr = " insert into `SIMPLE_BODY` ( SIMPLE_SIZE, SIMPLE_BODY) values( $fileLen, $data);"; #不预留参数而直接填充
my $sth = $dbh-> prepare( $insertStr ) or die(" Cannot prepare statement:", $dbh->errstr()," /n");
my $rc = $sth-> execute() or die(" Cannot execute statement:", $sth->errstr(), " /n");

 

你可能感兴趣的:(数据库,mysql,user,perl,insert,Warnings)