Delphi - FireDAC update MySQL table (blob) with dynamic array of bytes (TBytes)

66 views Asked by At

I would like to update a column in a MySQL table, which is a blob type, with a dynamic array of bytes (TBytes) using Delphi. I can't find any example or documentation that explains how to assign TBytes or some derivative it to the query paramaters.

I've tried converting the array of bytes to an AnsiString and then assign it. And it works. But I would really like to avoid first converting the array of bytes to a string.

DataString : AnsiString;
SetString(DataString, PAnsiChar(Data), Length(Data));
SettingsQuery.Params[0].DataType := ftBlob;
SettingsQuery.Params[0].AsBlob := DataString;

Here is my example code

procedure TForm1.Button1Click(Sender: TObject);
var
  SettingsQuery : TFDQuery;
  DBConnection : TFDConnection;
  Data : TBytes;
begin

  SetLength(Data, 512);

  DBConnection := TFDConnection.Create(nil);
  DBConnection.ConnectionDefName := 'MySQL';

  SettingsQuery := TFDQuery.Create(nil);
  SettingsQuery.Connection := DBConnection;

  try

    DBConnection.Connected := true;

    SettingsQuery.SQL.Text := 'UPDATE settings SET Data=:data WHERE ID=1;';

    SettingsQuery.Params[0].DataType := ftBlob;
    SettingsQuery.Params[0].As := Data; <----- How do I assign TBytes

    SettingsQuery.ExecSQL;

  finally

    FreeAndNil(SettingsQuery);
    FreeAndNil(DBConnection);

  end;

end;
0

There are 0 answers