Saving JPEG image into LONGBLOB field in mySQL

I want to save a screenshot into a mySQL table using Delphi code.

I have the jpeg image in a TImage component and want to write it using an SQL statement into a LONGBLOB field. I also want to be able to load it back from the db into an Image component.

I do not want to save the file locally and reference it via a field in the DB (I know many recommend this option)

Thanks.

WeeStinkerAsked:
Who is Participating?
 
JohnjcesConnect With a Mentor Commented:
Well, don't use those, but this "might" get you started or provide a hint since I use ADO.

See snippets below. These aren't tested as is but I use them similarly in one of my projects.

John
procedure ImageToTable;
var
 vlstream : TMemoryStream;
begin
 Screen.Cursor := crHourGlass;
 vlStream := TMemoryStream.Create();
 Image1.Picture.Bitmap.SaveToStream(vlStream);
 vlStream.Seek(0, soFromBeginning);
(ADOQuery1.FieldByName('MyPicture') as TBlobField).LoadFromStream(vlStream);
 vlStream.Free;
 Screen.Cursor := crDefault;
end;
 
procedure ImageFromTable;
var
 vlStream: TMemoryStream;
begin
 Screen.Cursor := crHourGlass;
 vlStream := TMemoryStream.Create();
 (ADOQuery1.FieldByName('MyPicture') as TBlobField).SaveToStream(vlStream);
 vlStream.Seek(0, soFromBeginning);
 Image1.Picture.Bitmap.LoadFromStream(vlStream);
 vlStream.Free;
Screen.Cursor := crDefault;
end;

Open in new window

0
 
JohnjcesCommented:
Are you using ADO or what database provider?

John
0
 
WeeStinkerAuthor Commented:
I am using myComponents by AidAim
0
 
WeeStinkerAuthor Commented:
Thanks, that gave me enough clues to get it working.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.