Chapter 8: Working with Binary (BLOB) Data

Overview

Databases can store more than plain text; they can store binary data as well. In database terminology, binary data is often referred to as BLOB (Binary Large Object). Binary data is handled a bit differently because you typically don't enter the binary data by hand for a file. Instead, an interface that allows you to choose a file to be uploaded is very helpful.

While thinking about this chapter, avoiding going back to the auction program we made in Chapter 7 was difficult. Bidding on items without seeing at least a small picture of what we are bidding on instills uneasiness. So this section builds on the auction program so that we can have images with the items.

You may ask yourself why you would want to store images in the database when the file system can hold the images just fine, and the database can hold a pointer to the files. This is a good question, and the truth of the matter is that both ways have their advantages. By storing the image location only, you save database space and don't have to deal with BLOB data. By storing image data in the database, you get the benefit of having a complete package because all data is in the database. Plus, when you back up your database, your images are backed up. Moreover, if your database is replicated across different servers, then your images will be replicated along with it.

Adding images to the auction program is quite easy. None of the existing code needs to be changed! We need only to add a program for fetching and displaying the images and to change the templates wherever we want an image to appear.

Our goal is to have images stored in the database and to be able to fetch the appropriate image when we choose. Also, we want to put a logo in the main header so that the auction program ends up looking something like Figure 8-1.

click to expand
Figure 8-1: Auction program with logo image

We will create three programs in this chapter. Two of them simply make it easier for auction administers to add items to the database. The third is used to fetch/display images. The only program needed for having images in our auction program is the third - the other two programs are not needed for the actual functionality of the auction program.

Since this book focuses on databases, not user interfaces we can skimp a bit and make a simple Web interface for adding the binary data to the database table. By 'skimp,' we can minimize the bells-and-whistles aspect but still have the functionality we need. The Web provides a universal GUI that can be accessed regardless of the backend database or the front-end client.



Perl Database Programming
Perl Database Programming
ISBN: 0764549561
EAN: 2147483647
Year: 2001
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net