3
votes

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: enter image description here

  • It contains the following: 3 TButton, 1 TOpenDialog and 1 TImage
  • Button Salveaza (Save) is Button3 and has ModalResult set to mrOk
  • Button Incarca Imagine (Load Image) is Button1
  • Button Cancel is Button2, has Cancel property ticked and has ModalResult set to mrAbort

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 a Data module, along with other things I'm using (like DataSource, 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 my TOpenDialog, 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!

1
try reversing the dbmodule.comenziQuery.Post and Stream.Freewhosrdaddy
You are using an Insert. This adds an extra record to the database. Is that what you mean to do?Dsm
@Dsm - No, I'm basically trying to update a row that already exists in the database, specifically it's pscreen field which is currently null, I'd like to add the image there. I'm selecting it with the mouse, in my dbgrid, the row I wish to add the image to and firing up this form. Every row has an ID number contained in a field named id, if that helps.t1f
That is my point. You are creating a new record and updating the blob field in that. Your current line will remain unchanged. You need to select the record represented by your line. I'm not familiar with ADO, but if it is a data-aware grid that you are using it might be as simple as deleting the insert line.Dsm
@whosrdaddy - nope, didn't change anything.t1f

1 Answers

0
votes

The following code worked and was successful in providing the functionality that I needed

procedure TaddImagineForm.Button3Click(Sender: TObject);
var
          Field: TBlobField;
          Stream: TStream;
begin
          dbmodule.comenziDataSet.Active := True;
          if (Image1.Picture.Graphic <> nil) then
          begin
             dbmodule.comenziDataSet.Edit;
             Field := TBlobField(dbmodule.comenziDataSet.FieldByName('pscreen')); // ensure it ís a blob
             Stream := dbmodule.comenziDataSet.CreateBlobStream(Field, bmWrite);
             try
                Image1.Picture.Graphic.SaveToStream(Stream);
             finally
                Stream.Free;
                dbmodule.comenziDataSet.Post;
             end;
          end;
end;


end.

Just replaced using the Query with using the DataSet and that seems to have solved it, also my original post / question was wrong as I've been confusing the problems. Still, good for anyone going through the same.

Thanks to Sertac Akyuz