I'm trying to save a .bmp from my PC to a database that contains a blob
field.
I am currently working with code from here:
How to insert image into database using TADOQuery Component Only
I've created a new form and added necessary components there.
This is how the form looks like:
- It contains the following: 3
TButton
, 1TOpenDialog
and 1TImage
- Button Salveaza (Save) is
Button3
and hasModalResult
set tomrOk
- Button Incarca Imagine (Load Image) is
Button1
- Button Cancel is
Button2
, hasCancel
property ticked and hasModalResult
set tomrAbort
The code for the Incarca Imagine (Button1) button is this:
procedure TaddImagineForm.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Image1.Picture.LoadFromFile(OpenDialog1.FileName);
end;
end;
The code for button named Salveaza (Button3) is:
procedure TaddImagineForm.Button3Click(Sender: TObject);
var
Field: TBlobField;
Stream: TStream;
begin
if dbmodule.comenziQuery.Active and (Image1.Picture.Graphic <> nil) then
begin
dbmodule.comenziQuery.Insert;
Field := TBlobField(dbmodule.comenziQuery.FieldByName('pscreen')); // ensure it ís a blob
Stream := dbmodule.comenziQuery.CreateBlobStream(Field, bmWrite);
try
Image1.Picture.Graphic.SaveToStream(Stream);
finally
Stream.Free;
dbmodule.comenziQuery.Post;
end;
end;
end;
- The
TSQLQuery
I'm using (named comenziQuery) is located on aData module
, along with other things I'm using (likeDataSource
,DataSet
,SQLConnection
and so on..) the Data module is named dbmodule - The column in the database that should hold the .bmp image is named pscreen and is set as a
mediumblob
type.
What should happen (in probably my newbie point of view)
- The
blob
field named pscreen for the selected row should now contain the .bmp I selected previously with myTOpenDialog
, by pressing the Incarca Imagine button
What's actually hapenning
- Once I click the Save button the form closes but the selected row doesn't get updated with the bmp image in it's blob field. No error, no warning, nothing. The field remains NULL for that row.
EDIT
Thanks to Dsm in the comments he pointed out that I'm using an insert, which adds a new record, not updates the one I'm selecting.
I'm referring to this line: dbmodule.comenziQuery.Insert;
Need to change the code somehow to update the record I'm selecting.
Extremely new to Delphi/SQL so I do apologize if it seems I'm banging my head against a wall and doing the exact opposite.
I'm using Rad Studio 10 Seattle, the database is MYSQL
, the database components are located on a data module named dbmodule and contain the following: TSimpleDataSet
, TSQLQuery
, TDataSource
, TSQLConnection
- they are dbExpress
components.
Thank you!