I am trying to write a jpg to a sqlite database and read it back. The read-back info says that the file maybe corrupted. The original size is 120,453 and the read-back file is 120,895 consistently.
use strict;
use warnings;
use util_routines;
use DBI;
my $my_picture_file = "c:\\users\\xxx\\work\\blob.jpg";
my $dbname = "a.dat";
my $dbh = util_routines::connect_to_sqlite($dbname);
create_my_blob();
open (my $MYFILE,'<', $my_picture_file) or die "Cannot open file....$
+!";
my $data;
{
local $/;
$data = <$MYFILE>;
close $MYFILE;
}
$data =~ s/^\s+//g;
$data =~ s/\s+$//g;
# insert image into table
my $sql = "INSERT INTO my_blob VALUES (?)";
my $sth = $dbh->prepare($sql) or die;
$sth->execute($data) or die;
# retrieve image from table
$sql = "SELECT image FROM my_blob";
$sth = $dbh->prepare($sql) or die;
$sth->execute() or die;
my $ref = $sth->fetchrow_hashref;
my $newdata = $$ref{'image'};
$newdata =~ s/^\s+//g;
$newdata =~ s/\s+$//g;
open (my $OUTPUT, '>', "output_blob.jpg") or die "$!\n";
print $OUTPUT $newdata;
close $OUTPUT;
$sth->finish;
$dbh->disconnect;
sub create_my_blob {
$dbh->do('drop table my_blob');
my $sql = qq/create table my_blob (image blob)/;
$dbh->do($sql) or die "\n\n$DBI::errstr";
print "Created my_blob\n";
return;
}
__END__
OS os win7 with Strawberry Perl v5.24.1
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.