Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MySQL 8
FireDac Components (FDQuery + FDPhyMySqlLink)

Table with two field a BLOB and a TINYBLOB

The image is 7384 byte

When execute the query the error is :
[Firedac][Phys][MySql] data too long for column immagine at row 1


from the MySqlWordBench the operation on Field Blob /TinyBlob the Image is Inserted on DataBase

What I have tried:

 MemStream: TMemoryStream;

begin
  Image1.Picture.LoadFromFile(sFile);

  MemStream := TMemoryStream.Create;
  try
    Image1.Picture.SaveToStream(MemStream);
    MemStream.Seek(0,0);
    FDQuery1.SQL.Text := 'INSERT INTO tb_test (immagine1) VALUES (:img)';
    FDQuery1.ParamByName('img').LoadFromStream(MemStream, ftBlob);
    FDQuery1.ExecSQL;

  finally
    MemStream.Free;
  end;
Posted
Updated 22-Jul-19 7:50am

1 solution

Fixed (almost, still a problem with 5 Mb files - eg: 4.987.904 should go smoothly on MEDIUMBLOB instead
Lost Connection during query... to understand, already with 2.7 Mb files you
has the exception).


in MySql
{
TINYBLOB   :     maximum length of 255 bytes
BLOB       :     maximum length of 65,535 bytes
MEDIUMBLOB :     maximum length of 16,777,215 bytes
LONGBLOB   :     maximum length of 4,294,967,295 bytes
}

procedure Tfrm_Articoli.InserisceImmagine(sFile: string);
var jp:TJpegImage;
    MemStream: TMemoryStream;
begin
  sFile := '<foto da inserire>';

  FDQuery1.Open;
  FDTransaction1.StartTransaction;

  //Medium Blob
  sFile := '<foto da inserire>';

  FDTransaction1.StartTransaction;

  try
    jp := TJpegImage.Create;
    jp.LoadFromFile(sFile);
    FDQuery1.Active := true;
    FDQuery1.Insert;
    FDQuery1.FieldByName('immagine1').Assign(jp);
    FDQuery1.Post;
    FreeAndNil(jp);
    FDTransaction1.Commit;
  except
    FDTransaction1.RollBack;
  end;

//versione SQL

  MemStream := TMemoryStream.Create;

  sFile := '<foto da inserire>';
  Image1.Picture.LoadFromFile(sFile);

  Image1.Picture.SaveToStream(MemStream);
  MemStream.Seek(0,0);
  FDQuery1.SQL.Text := 'INSERT INTO tb_test (immagine1) VALUES (:img)';
  FDQuery1.ParamByName('img').LoadFromStream(MemStream, ftBlob);
  FDQuery1.ExecSQL;

..//
end; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900