#! 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"); |