Delphi透過MySQL存入檔案備份

MySQL存Blog的大小
TINYBLOB – 255 bytes
BLOB – 65535 bytes
MEDIUMBLOB – 16,777,215 bytes (2^24 – 1)
LONGBLOB – 4G bytes (2^32 – 1)

透過資料庫存取的方式重點:

TBlobField – LoadFromStream、SaveToStream
Tmemorystream – LoadFromFile、SaveToFile(.Position :=0)

存入資料庫

var
  astream : Tmemorystream;
begin
  AStream := TMemoryStream.Create;
  try
    astream.LoadFromFile(filename);
    AStream.Position := 0;
    if adotable1.Active then
    begin
      adotable1.Edit;
      TBlobField(adotable1.FieldByName('t2')).LoadFromStream(AStream);
      adotable1.Post;
    end;
  finally
    AStream.free;
  end;
end;

存回檔案

var
  Ms:Tmemorystream;
begin
  Ms := Tmemorystream.Create;
  try
    if adotable1.Active then
    begin
      TBlobField(adotable1.FieldByName('t2')).SaveToStream(Ms);
      Ms.Position := 0;
      MS.SaveToFile(sname);
    end;
  finally
    ms.Free;
  end;
end;

MySQL預設的BLOB為1M
別忘了 My.ini調整
max_allowed_packet = XXM