Reading Blob data using Perl

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading Blob data using Perl

Brian Rowlands (Greymouth High School)
Hi
I'm hoping someone can help me explain how to fix a problem I have with
reading a Blob field from a SQLite DB using Perl. My long term aim is to
not save it to a file but use it within my application and store it as a
label bitmap. However, first things first.

This code reads the Blob but when I view the bmp file created it is
distorted - as though the bits have been wrapped around and ones I'd
expect on the right are on the left of the image in the prevw. Looking
at the icon in the SQLite DB all is well.

#!/usr/bin/perl -w
use strict;
use DBI;
use Cwd;
use Win32::GUI::BitmapInline ();
our $datafile   = getdcwd()."\\mydatabase.db";

# suck icon from SQLite database,
my @array;
my $dbh = DBI-> connect("dbi:SQLite:dbname=$datafile","","",{});    #
database handle
my $sql = "SELECT icon FROM icons WHERE name = 'logo'";
my $sth = $dbh-> prepare($sql);                                     #
statement handle

# must have this next line but DONT KNOW WHY
my $numrows = $sth->execute;

open OUTPUT, ">output.bmp";
my $ref = $sth->fetchrow_hashref;
my $newdata = $$ref{'icon'};
print OUTPUT $newdata;
close OUTPUT;

$sth->finish;
undef $sth;
$dbh->disconnect();


Q1. Why is the line my $numrows = $sth->execute; essential? If it is not
present I can't view the image file at all and I get a message that
preview is not available.
Q2. How do I fix it so the bmp file displays perfectly?


Thanks
Brian Rowlands
We must accept finite disappointment, but we must never lose infinite
hope.
Martin Luther King Jr. </quotes/k/martinlutherking/>  



Reply | Threaded
Open this post in threaded view
|

Re: Reading Blob data using Perl

Puneet Kishor-2
can't help you all the way, but below are a few comments --

On 10/16/07, Brian Rowlands  (Greymouth High School)
<[hidden email]> wrote:

> Hi
> I'm hoping someone can help me explain how to fix a problem I have with
> reading a Blob field from a SQLite DB using Perl. My long term aim is to
> not save it to a file but use it within my application and store it as a
> label bitmap. However, first things first.
>
> This code reads the Blob but when I view the bmp file created it is
> distorted - as though the bits have been wrapped around and ones I'd
> expect on the right are on the left of the image in the prevw. Looking
> at the icon in the SQLite DB all is well.
>
> #!/usr/bin/perl -w
> use strict;
> use DBI;
> use Cwd;
> use Win32::GUI::BitmapInline ();
> our $datafile   = getdcwd()."\\mydatabase.db";
>
> # suck icon from SQLite database,
> my @array;
> my $dbh = DBI-> connect("dbi:SQLite:dbname=$datafile","","",{});    #
> database handle
> my $sql = "SELECT icon FROM icons WHERE name = 'logo'";
> my $sth = $dbh-> prepare($sql);               # statement handle
>
> # must have this next line but DONT KNOW WHY
> my $numrows = $sth->execute;

The line above is where you actually make your program do some work.
After all those declarations, you have to execute the statement, and
that is precisely what you are doing above. That is why you need the
above line, and without it nothing will work.

>
> open OUTPUT, ">output.bmp";
> my $ref = $sth->fetchrow_hashref;
> my $newdata = $$ref{'icon'};

I would write the above line as

my $newdata = $ref->{icon};

now, somewhere here, don't you require some binmode magic like so,
particularly on Windows

binmode OUTPUT;

> print OUTPUT $newdata;
> close OUTPUT;
>
> $sth->finish;
> undef $sth;
> $dbh->disconnect();
>
>
> Q1. Why is the line my $numrows = $sth->execute; essential? If it is not
> present I can't view the image file at all and I get a message that
> preview is not available.
> Q2. How do I fix it so the bmp file displays perfectly?
>
>
> Thanks
> Brian Rowlands
> We must accept finite disappointment, but we must never lose infinite
> hope.
> Martin Luther King Jr. </quotes/k/martinlutherking/>
>
>
>
>


--
Puneet Kishor

-----------------------------------------------------------------------------
To unsubscribe, send email to [hidden email]
-----------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

FW: Reading Blob data using Perl

Brian Rowlands (Greymouth High School)
In reply to this post by Brian Rowlands (Greymouth High School)
Magic Kishor

I'm learning a lot as I study Perl and now SQLite and your invaluable
assistance did the trick.

The key lines now read:

my $newdata = $ref->{icon};
binmode OUTPUT;
print OUTPUT $newdata;

Many thanks.

-----Original Message-----
From: P Kishor [mailto:[hidden email]]
Sent: Wednesday, 17 October 2007 3:07 p.m.
To: [hidden email]
Subject: Re: [sqlite] Reading Blob data using Perl

can't help you all the way, but below are a few comments --

On 10/16/07, Brian Rowlands  (Greymouth High School)
<[hidden email]> wrote:
> Hi
> I'm hoping someone can help me explain how to fix a problem I have
> with reading a Blob field from a SQLite DB using Perl. My long term
> aim is to not save it to a file but use it within my application and
> store it as a label bitmap. However, first things first.
>
> This code reads the Blob but when I view the bmp file created it is
> distorted - as though the bits have been wrapped around and ones I'd
> expect on the right are on the left of the image in the prevw. Looking

> at the icon in the SQLite DB all is well.
>
> #!/usr/bin/perl -w
> use strict;
> use DBI;
> use Cwd;
> use Win32::GUI::BitmapInline ();
> our $datafile   = getdcwd()."\\mydatabase.db";
>
> # suck icon from SQLite database,
> my @array;
> my $dbh = DBI-> connect("dbi:SQLite:dbname=$datafile","","",{});    #
> database handle
> my $sql = "SELECT icon FROM icons WHERE name = 'logo'";
> my $sth = $dbh-> prepare($sql);               # statement handle
>
> # must have this next line but DONT KNOW WHY my $numrows =
> $sth->execute;

The line above is where you actually make your program do some work.
After all those declarations, you have to execute the statement, and
that is precisely what you are doing above. That is why you need the
above line, and without it nothing will work.

>
> open OUTPUT, ">output.bmp";
> my $ref = $sth->fetchrow_hashref;
> my $newdata = $$ref{'icon'};

I would write the above line as

my $newdata = $ref->{icon};

now, somewhere here, don't you require some binmode magic like so,
particularly on Windows

binmode OUTPUT;

> print OUTPUT $newdata;
> close OUTPUT;
>
> $sth->finish;
> undef $sth;
> $dbh->disconnect();
>
>
> Q1. Why is the line my $numrows = $sth->execute; essential? If it is
> not present I can't view the image file at all and I get a message
> that preview is not available.
> Q2. How do I fix it so the bmp file displays perfectly?
>
>
> Thanks
> Brian Rowlands
> We must accept finite disappointment, but we must never lose infinite
> hope.
> Martin Luther King Jr. </quotes/k/martinlutherking/>
>
>
>
>


--
Puneet Kishor

------------------------------------------------------------------------
-----
To unsubscribe, send email to [hidden email]
------------------------------------------------------------------------
-----


-----------------------------------------------------------------------------
To unsubscribe, send email to [hidden email]
-----------------------------------------------------------------------------