Homer Jones
Homer Jones

Reputation: 53

How do I add various document types to a Firebird BLOB field with Delphi

Background: Our product includes a document management system that allows users to save and retrieve various document types (.PDF, .DOC, .JPG, .PNG, .TIF, etc.). Currently there is a record in the database that stores document information, but the documents themselves are stored on the server's hard drive. The database record includes the disk file name so it can be retrieved and displayed. We now need to migrate all the documents into the database. For some of our clients, that could mean thousands of individual document files. We are aware of the pros and cons of having the docs in the DB, but still need to migrate them to the DB.

The plan: I plan to loop through the table containing all the document information. Each record now has a new, empty BLOB field, and a text field to hold the file extension (so that I know how to display it later). The plan is to retrieve the file and save it in the new BLOB field, along with its extension, as I loop to the next record/document.

Question: The articles I've read so far use Streams to create a totally new record. The type of stream depends on the article (TMemoryStream, TFileStream, TBlobStream). Since I already have a record, I'm interested in knowing the best way to fill the BLOB field. I would appreciate it if someone can offer an example of Delphi code that would add such a document to a Firebird database. I've been coding for too many years to count, but have never had the opportunity to work with BLOB streams.

Upvotes: 1

Views: 206

Answers (3)

rstrelba
rstrelba

Reputation: 1964

My 1 cent. You win nothing using scheme with documents in blobs. All you will have are all eggs in one basket. I returned back scheme with docs on filesystem when faced backup hell, you have to backup all information again and again during 1 hour instead of speding 1 min for backup main database and 1 min for syncing new or updated docs on filesystem. If you have decided to go to blobs, just one advice, keep saving new documents to disk with keeping file name in db record but save it in blob. This helps you to return back to old scheme :-)

Upvotes: 0

Gabriel P.
Gabriel P.

Reputation: 11

Iterate through your Query:

Query.close;
Query.sql.text := 'select * from ATable';
Query.open;
While not Query.eof do
Begin
if FileExists(Query.FieldbyName('DocumentName').asstring) then
Begin
Query.Edit;
TBlobField(Query.Fieldbyname('Document').LoadFromFile(
Query.FieldbyName('DocumentName).asstring);
Query.post;
end;
Query.next;
end;

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 597941

You can use your DataSet's CreateBlobStream() method to access a BLOB field of an existing record. You can write whatever you want to that stream.

Upvotes: 2

Related Questions