mysql – File upload (images – jpg in BLOB-field)

Question:

I am trying to load a file into a database (MySQL), into a blob-field (small, up to 50kb picture – jpg format).

But something goes wrong with me:

var
  ImgPath: String;

begin  
  ImgPath:=ExtractFilePath(ParamStr(0))+'data\photo.jpg';
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.ADD('UPDATE exam_users SET `photo`=(:fphoto) WHERE `number` = 94');
  ADOQuery1.parameters.parambyname('fphoto').LoadFromFile(ImgPath, ftBlob);

  ADOQuery1.ExecSQL;
  ADOQuery1.Close;
end;

also tried it like this:

var
  ImgPath: String;
  fs:TFileStream;

begin    
  ImgPath:=ExtractFilePath(ParamStr(0))+'data\photo.jpg';
  fs :=TFileStream.Create(ImgPath, fmOpenRead or fmShareDenyWrite);
  ADOQuery1.SQL.ADD('UPDATE exam_users SET `photo`=(:fphoto) WHERE `number` = 94');
  ADOQuery1.Parameters.ParamByName('fphoto').LoadFromStream(fs, ftBlob);
  ADOQuery1.ExecSQL;
  fs.Free;
  ADOQuery1.Close;
end;

All these two examples give the same error:

Project Project2.exe raised exception class EOleException with message 'Arguments are of the wrong type, out of range, or conflict with each other.'

Answer:

Something tells me that the field type is not exactly ftBlob, I would experiment with other values. As I understand it, you have a text field type in your database?

When working with a stream, try setting the position to 0 again.

fs := TFileStream.Create...
fs.Position = 0;

In extreme cases, you can write something like this.

ImgPath := ExtractFilePath(ParamStr(0))+'data\photo.jpg';
ADOQuery1.SQL.Text := 'select `photo` from exam_users where `number` = 94';
ADOQuery1.Open;
TBlobField(ADOQuery1.FieldByName('photo')).LoadFromFile('c:\sample_2.jpg');

But that doesn't solve your problem. And if there is already data in the database, they are first drawn to the client, and then updated.

Scroll to Top