Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary dBase memo fields

Status
Not open for further replies.

cvgalante

MIS
Jun 27, 2002
18
0
0
US
I've been trying to modify a binary memo field in a dBase file using a variety of methods and can't get any to work. I was trying the BlobStream methods as in the Help examples and they don't run as written. I get errors when I try to create a TBlobStream variable using the Ttable CreateBlobStream method - it seems to only want to create a TStream!
Am I using the right approach or is there an easier way?

Thanks,
 
What is the actual code that doesn't seem to work ?

Andrew
Hampshire, UK
 
Good question. I have changed it so many times and I cannot get a clean compile anyway. Here is one example. Assume a TTable named dbf1 pointed to a dBase file with a memo field named TREEDATA.

tsread,
tswrite : tblobstream;
begin
with dbf1 do begin
open;
tsread:=dbf1.CreateBlobStream(dbf1.FieldByName('TREEDATA'),bmRead);
.... some code to modify tsread and move it to tswrite
edit;
tswrite:=dbf1.CreateBlobStream(dbf1.FieldByName('TREEDATA'),bmWrite);

This won't compile because tsread and tswrite are tblobstream and dbf1.createblobstream wants to make a Tstream. When I make them Tstreams, other operations don't work.

Do you have example code that has succesfully modified binary memo fields?

Thanks,

cvgalante
 
You're right, the code given in the help file example doesn't compile (at least not in Delphi 4, which I'm using). Looking at the code of CreateBlobStream, it does create a TBlobStream, but then bizarrely returns it as a TStream. The obvious solution to your rpoblem is therefore to create the TBlobStreams yourself, i.e.

Code:
var
  tsread, tswrite : tblobstream;
begin
  with dbf1 do
  begin
    open;
    tsread := TBlobStream.Create(dbf1.FieldByName('TREEDATA') as TBlobField, bmRead);
    //.... some code to modify tsread and move it to tswrite
    edit;
    tswrite := TBlobStream.Create(dbf1.FieldByName('TREEDATA') as TBlobField, bmWrite);

Steve
 
Thanks, but I have been through this and a dozen other variations. This doesn't compile and gives the message:
Incompatible types: 'TBlobField' and 'TField'

Some code I can get to compile but it doesn't write the data back to the memo field properly - at least the original application no longer interprets it properly.

I guess I am thinking this is just broken. If anyone has a code sample that can read, modify and write back a binary memo field to dBase (preferably Delphi 5) I would love to see it.

Thanks,

cvgalante
 
Hmmmm, the code I posted compiles for me. Make sure that you typecast your field as a TBlobField when you pass it into the TBlobField.Create procedure , e.g.

Code:
tsread := TBlobStream.Create(dbf1.FieldByName('TREEDATA') as TBlobField, bmRead);

Steve
 
Well, DUH! I was trying to typecast it by suffixing it with .ASBLOB which, of course, did not work.

It does compile but the process still trashes the memo contents. Here is the procedure at this point:

procedure TForm1.ChangeStatusBufClick(Sender: TObject);
var
buf : array[1..100000] of byte;
slen : longint;
i : integer;
tsread,
tswrite : tBLOBstream;
begin
dbf1.open;
while dbf1['USERLINK']<>2 DO next;
tsread:=TBlobStream.Create(dbf1.FieldByName('TREEDATA') as TBlobField,bmRead);
slen:=tsread.size;
tsread.ReadBuffer(buf,slen);
tsread.free;
for i := 0 to slen div 196 - 1 do begin
buf[i*196+141] := 1;
end; // loop through s
dbf1.edit;
tswrite:=TBlobStream.Create(dbf1.FieldByName('TREEDATA') as TBlobField,bmReadWrite);
tswrite.WriteBuffer(buf,slen);
tswrite.Free;
dbf1.post;
dbf1.close;
end;

I have actually tried writing buf to a file just before the tswrite code and at that point it is correct. But after I execute this procedure, the memo field does not resemble the old one. Any ideas?

Thanks,

cvgalante
 
I don't have dBase, but I tried your code using a Paradox table instead, and it worked fine. I ran the ChangeStatusBufClick procedure a couple of times, and on the second run through when looping through s I checked the value of each byte that was changed, and they were all 1. I checked other bytes as well and they were all still at the initial value I gave them.
I guess this means I can't really help you any further, but at least you know that the problem is not in your code!

Steve
 
Well, that's encouraging - sort of. :) I really appreciate all your help. I still can't help but wonder if something is broken on the dBase side (Paradox stuff always seems to work better in Delphi), but if I learn anything new I'll send it along.

cvgalante
 
If anyone is still watching this thread, I think the problem is nulls in the binary memo field (which have to be there - the original application is using the memo field as a very large array of structured records). I have used a TBlobField to SaveToFile the binary data and it is formatted fine. When I use the TBlobField to LoadFromFile and SaveToFile again, the data is lost. Using a TGraphicField will not compile when trying to assign the dataset blob field to it. Any other ideas?

Thanks,

cvgalante
 
What is the actual code that doesn't seem to work ?

It would be a lot easier to read if you could use TGML to format your code in the reply. See Steve's example above.

Andrew
Hampshire, UK
 
Late breaking news. I found the problem. The TBlobField defaults to Transliterate = True. I set it to False and it allowed the nulls to be written with no problem. Of course, you would think that a Blob field would default to this!!

Thanks to everyone who helped with this issue,

cvgalante

P.S. Sorry about the formatting of the code - I was just desperately typing various things to try them out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top