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!

MySQL Blob Field 1

Status
Not open for further replies.

kiddjr

Programmer
Sep 17, 2004
5
0
0
US
I'm trying to insert a pdf file into a blob field within mysql. When I run the following code I get no compiler errors, it just doesn't write any data to the database.

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
ReadFile: TFileStream;
BlobField: TBlobField;
BS: TStream;
begin
ADODataSet1.CommandText := 'SELECT * FROM MachineParts WHERE ID = 3';
ADODataSet1.Active := true;
ADODataSet1.Edit;
BlobField := ADODataSet1.FieldByName('DRAWING') AS TBlobField;
BS := ADODataSet1.CreateBlobStream(BlobField,bmWrite);
BS.Seek(0, soFromBeginning);
ReadFile := TFileStream.Create('C:\Line-Card.pdf', fmOpenRead or fmShareDenyWrite);
BS.CopyFrom(ReadFile, ReadFile.Size);
ReadFile.Free;
BS.Free;
ADODataSet1.Active := false;
end;

BTW, I'm running Delphi 2006, MySQL5.0, and using the ODBC connector 3.51. I can read, insert, update all other data just fine. This BLOB thing is just giving me a fit.

If anyone can point me in the right direction I sure would appreciate it.

Thanks
 
What about the post in ADODataset1 after the BS.CopyFrom? Is it not needed?

buho (A).
 
I've tried putting the post there, I was thinking the same thing. But when I do that it raises an exception saying that the dataset is not in edit or insert mode.

Don't quite understand that one since a few lines up it is being told to go into edit.

Thanks,
kiddjr
 
Me neither. The Edit is there in the code you posted; looked up some examples in D6 help and, of course, they use Post.

Are you sure your dataset is not empty?

buho (A).


 
this is how I save file in an access DB via TADOTable:

Code:
var SaveFileToTable(Table: TADOTable; FileField:TBlobField; Filename: string);

var
  fS  : TFileStream;
begin
  fs:=TFileStream.Create(Filename, fmOpenRead);
  try
   Table.Edit;
   FileField.LoadFromStream(fs);
   Table.Post;
  finally
   fs.Free;
  end;
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks for the replies. I finally got it using basically the code whosrdaddy supplied.

Here is what mine looks like:

Code:
procedure TForm1.Button2Click(Sender: TObject);
var
fs: TFileStream;
BlobField: TBlobField;
begin
fs := TFileStream.Create('C:\Line-Card.pdf', fmOpenRead);
ADOQuery1.SQL.Text := 'SELECT * FROM MachineParts WHERE TAG_PART_NUMBER = ''M00000100''';
ADOQuery1.Active := True;
BlobField := ADOQuery1.FieldByName('DRAWING') AS TBlobField;
try
  ADOQuery1.Edit;
  BlobField.LoadFromStream(fs);
  ADOQuery1.Post;
finally
  fs.Free;
  ADOQuery1.Active := false;
end;
end;

Again thanks for all the help. Maybe someone else will find this useful.

Thanks,
kiddjr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top