Subject Re: [firebird-support] Insert into DAtabase from BLOB file
Author Helen Borrie
At 12:10 PM 27/07/2005 +0200, you wrote:
>I mean, if I extract datas from a table with some program, it stores the
>datas in a text-like file, and another binary file to store binary (blod)
>datas.

?

>But what,if I have some pictures, and I want to insert them into a
>database. How shall I make this binary file,

In any way you like: a graphics program, digital camera - use any
application that can create the kind of binary object you want to store.

>and how can I find those IDs?

You don't. You define a table that has a blob column defined for that data,
e.g.

create table blobstore (
row_id integer not null primary key,
description varchar(50),
imageblob blob sub_type 1);

Then, in your application, you set up an INSERT statement, to insert a row
containing the blob:

insert into blobstore(id, description, imageblob)
values (?,?,?);

When the statement is prepared, the engine passes a structure back to your
application, that tells it to place an integer in place of the first
parameter, a varchar(50) in the second and a blob in the third.

Your application then assigns values to the parameters. For example, it
might fetch a value from a generator for the first parameter, read some
user input for the second one and assign the contents of an image file to
the third one.

The first two are obvious. The third one needs a client-side method to
read the image file, byte-by-byte, into a blob structure that has been
pre-defined in your application code. That method entirely depends on what
language interface you use.

When the database engine receives the INSERT request, it allocates a page
for your blob and writes a temporary blob id for it. It will keep
allocating pages until it has received all of the data from your client
buffer. That temporary blob id is written onto the data record. If/when
the operation gets committed, the engine assigns a permanent blob id to the
blob and updates the data record.

The next time an application accesses that record, the blob will be
available. Your SQL does not know or touch the blob id; you access the
blob column by name, just like any other data column. You can bring that
data across to your application and display it by some appropriate means.

If you post a request to "update" the blob, you don't update that stored
blob at all. A completely new blob with a new blob id will be created and,
when the work is committed, the old blob is simply set aside for garbage
collection along with the old version of the data record.

In case you thought there was a way to make multiple data rows refer to the
same blob id, there is none. If you have to support a relationship like
that, make a foreign key between the many "user" rows in one table and the
primary key of a single row in your blobstore table.

./heLen