Perl: insert binary fields into the MySQL database

Source: Internet
Author: User
Perl: inserts binary fields into the MySQL database, and returns the value of the automatically increasing field of the inserted record at,-10-27.

#! 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 (); # Calculate the MD5 Value

Seek (hfile, 0, 0); # adjust the file pointer to the file header. required steps
My $ data; # store file content
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 (?,?); "; # Reserved parameter location
My $…… = $ DBH-> prepare ($ insertstr) or die ("cannot prepare statement:", $ DBH-> errstr (), "/N ");
My $ rc = $ something-> execute ($ Filelen, $ data) Or die ("cannot execute statement:", $ something-> errstr (), "/N ");
My $ id = $ DBH->Last_insert_id(UNDEF, UNDEF, "'simple _ body'", "simple_body_key ");
# Ignore the first two parameters. 3rd are data table names and 4th are column names with auto-increment attributes.

Print 'insert _ id = ', $ id, "/N ";

Warn ($ DBI: errstr) If $ DBI: Err;
$ DBH-> disconnect ();

========================================================== ==========
Note: If the database insert statement does not require parameter reservation,The operation fails.Why not found?
My $ insertstr = "insert into 'simple _ body' (simple_size, simple_body) values ($ Filelen, $ data); "; # Fill directly without reserved Parameters
My $…… = $ DBH-> prepare ($ insertstr) or die ("cannot prepare statement:", $ DBH-> errstr (), "/N ");
My $ rc = $ something-> execute () or die ("cannot execute statement:", $ something-> errstr (), "/N ");

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.