Actually, as a final "fix" to the whole thing -- do you actually need the latest image file to be kept on the file system, or are you just keeping it there to compare against the next fetch? If you do not need the image on the file system, you can bypass that altogether and just use the database:

#!perl -w use strict; use DBI; use LWP::Simple; my $img_url = "http://weather.unisys.com/satellite/sat_ir_west.gif"; my $dbh = DBI->connect( 'dbi:mysql:weather_images', 'user', 'secret', { RaiseError => 1, AutoCommit => 1 } ); my ($min, $hour, $day, $mon, $year) = (localtime())[1..5]; $year += 1900; my $img_name = "$mon-$day-$year-$hour:$min"; my $img_data = get($img_url); die("failed to fetch image from internet!\n") unless ( defined($img_data) ); # fetch the last row we added to the table my $latest_entry = $dbh->selectrow_arrayref( 'SELECT image_file FROM gw_ir ORDER BY image_id DESC LIMIT 1' ); # either no data in database or the file contents # have changed since the last update if (not defined($latest_entry) or $latest_entry->[0] ne $img_data) { $dbh->do( 'INSERT INTO gw_ir (image_name, image_file) VALUES(?,?)', undef, $img_name, $img_data ); warn("File was not current; updates made.\n"); } else { warn("File was current, no updates made.\n"); } $dbh->disconnect();

And even if you do need the file system file, it might even be better to use the database anyway and then output the file:

#!perl -w use strict; use DBI; use LWP::Simple; my $img_url = "http://weather.unisys.com/satellite/sat_ir_west.gif"; my $local_img = "/usr/home/mhearse/weather_images/latest.gif"; my $dbh = DBI->connect( 'dbi:mysql:weather_images', 'user', 'secret', { RaiseError => 1, AutoCommit => 1 } ); my ($min, $hour, $day, $mon, $year) = (localtime())[1..5]; $year += 1900; my $img_name = "$mon-$day-$year-$hour:$min"; my $img_data = get($img_url); die("failed to fetch image from internet!\n") unless ( defined($img_data) ); # fetch the last row we added to the table my $latest_entry = $dbh->selectrow_arrayref( 'SELECT image_file FROM gw_ir ORDER BY image_id DESC LIMIT 1' ); # either no data in database or the file contents # have changed since the last update if (not defined($latest_entry) or $latest_entry->[0] ne $img_data) { open( my $fh, '>', $local_img ) or die("could not open file: $!"); binmode($fh); print $fh $img_data; $dbh->do( 'INSERT INTO gw_ir (image_name, image_file) VALUES(?,?)', undef, $img_name, $img_data ); warn("File was not current; updates made.\n"); } else { warn("File was current, no updates made.\n"); } $dbh->disconnect();

In reply to Re^3: Working with DBI and BLOB by saskaqueer
in thread Working with DBI and BLOB by mhearse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.