I'm trying to write a simple program to grab satellite images from the web. They are updated about every hour. After fetching the image, I check to see if it is current and store it in a MySQL database. Only one table in the database:
mysql> describe gw_ir; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | image_id | int(11) | | PRI | NULL | auto_increment | | image_name | varchar(50) | YES | | NULL | | | image_file | mediumblob | YES | | NULL | | +------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
The script is primitive, as this is I am definetely a DBI amateur:
use LWP::Simple; use DBI; $url = "http://weather.unisys.com/satellite/sat_ir_west.gif"; $file = "/usr/home/mhearse/weather_images/sat_ir_west.gif"; $newfile = "/usr/home/mhearse/weather_images/latest.gif"; ($min, $hour, $day, $month, $year) = (localtime)[1,2,3,4,5]; $image_name = "$month-$day-$year-$hour:$min"; getstore($url, $file); if (-s $file != -s $newfile) { rename $file, $newfile; $img_data = getFile($newfile); $dbh = DBI->connect("dbi:mysql:weather_images", "user", "********") or die("Error! $!\nAborting"); $sql = qq(INSERT INTO gw_ir (image_name, image_file) VALUES ("$image_name", "$image_file")); $sth = $dbh->prepare($sql); $sth->execute or die("\nError executing SQL statement! $DBI::errstr +"); $dbh->disconnect; } else { print "file is current\n"; } sub getFile { my($file) = @_; my $fh; unless (open $fh, "<", $file) { die "Error opening $file : $!\n"; } my $data; { local $/ = undef; $data = <$fh>; } close $fh; return $data; }
The main problem I'm having is with the storing the gif as BLOB. It doesn't seem to be working. Am I doing something obviously wrong? Also, I'm downloading the image every 60 minutes (it's only about 100k). Then comparing it to the previous to see if it is newer. Is there a better way to see if the image is current? I appreciate any help.

In reply to 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.