Perl: 向mysql資料庫插入二進位欄位

來源:互聯網
上載者:User
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");

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.