Bennie Coetzer
Bennie Coetzer

Reputation: 21

Firedac not replacing images in firebird database

I am trying to replace images in a firebird database using Firedac and delphi 11. All fields do get updated but the image fields IMAGE and IMGTHUMB do not change. When adding new records the similar code works fine but I do need to replace some images with others and have no success so far. The savetofile is primarily there for debugging to verify what I am trying to save and would probably be changed to savetostream later. Some insights would be appreciated. This same code works elsewhere when only a single image is replaced and not a batch.

      if SaveAction = 'UPDATE' then
      begin
//MUST DO AN UPDATE OF FIELDS AND ONLY if NECESSARY AN IMAGE
        SQLStr := 'UPDATE IMAGES SET';
        SQLStr := SQLStr +
        ' FORMNAME = ' + '''' + myImgObject.FormName + '''' +
        ', IMGTYPE = ' + '''' + inttostr(myImgObject.SpcPicInt) + '''' +
        ', IMGSOURCE = ' + '''' + myImgObject.ImgSource + '''' +
        ', IMGLABEL = ' + '''' + ImageLabel + '''' +
        ', IMGDESCR = ' + '''' + myImgObject.ImageDescr + '''' +
        ', IMGSCALE = ' + '''' + formatfloat('0.000', myImgObject.ImgScale) + '''' +
        ', IMGTITLE = ' + '''' + myImgObject.ImgTitle + '''' +
        ', IMGFILE = ' + '''' + theFilename + '''' +
        ', DATECHANGED = ' + '''' + MakeFireBirdDateTime(Now) + '''' +
        ', EDITOR = ' + '''' + '' + '''' +
        ' WHERE IMAGE_ID = ' + '''' + inttostr(ImageID) + '''';
        myQry.SQL.Add(SQLStr);
        try
          myQry.ExecSQL;
        except
          on E: Exception do
          begin
            ShowMessage('Exception class name = ' + E.ClassName);
            ShowMessage('Exception message = ' + E.Message);
            ShowMessage('DataInsert Error (Image)');
          end;
        end;

        if (myImgObject.ImgUpdate) AND (myImgObject.Bitmap.Width > 0) then
        begin
          with myQry do // Can also use the SQLDataset
          begin
            aBitmap := TBitmap.Create(10,10);
            aBitmap.Assign(myImgObject.Bitmap);
            CodecParams.Quality := 100;
            aBitmap.SaveToFile(ScratchPath + 'i1.jpg', @CodecParams);
            // Create a thumb
            ThumbW := 100;
            ThumbH := round(aBitmap.Height/aBitmap.Width) * ThumbW;
            ThumbBitmap := aBitmap.CreateThumbnail(ThumbW,ThumbH);
            ThumbBitmap.SaveToFile(ScratchPath + 'i2.jpg', @CodecParams);

            SQL.Clear;
            SQLStr := ('UPDATE IMAGES SET IMAGE = :IMAGE' + ', IMGTHUMB = :IMGTHUMB' +
              ' WHERE IMAGE_ID = ' + '''' + inttostr(myImgObject.ImgID) + '''');
            myQry.SQL.Add(SQLStr);
            Params[0].LoadFromFile(ScratchPath + 'i1.jpg', ftBlob);
            Params[1].LoadFromFile(ScratchPath + 'i2.jpg', ftBlob);
            try
              myQry.ExecSQL;
            except
              on E: Exception do
              begin
                ShowMessage('Exception class name = ' + E.ClassName);
                ShowMessage('Exception message = ' + E.Message);
                ShowMessage('DataInsert Error (Image)');
              end;
            end;
            myQry.close;
            aBitmap.Free;
            ThumbBitmap.free;
          end;
        end;

      end

Upvotes: 0

Views: 103

Answers (0)

Related Questions