insert blob from tmemorystream delphi
insert blob from tmemorystream delphi
Hi! How INSERT into BLOB one tmemorystream using DELPHI.
thanks.
thanks.
You can try a code like this:
You can also see the Picture demo included in MyDAC General Demo. It demonstrates how to load BLOB values from files.
Code: Select all
var
BlobField: TBlobField;
begin
...
BlobField := MyQuery.FieldByName('Picture') as TBlobField;
BlobField.LoadFromStream(FileName);
...
end;procedure TForm1.Button1Click(Sender: TObject);
var
stream: TMemorystream;
begin
if not OpenImageEnDialog1.Execute then exit;
stream := TMemorystream.Create;
stream.LoadFromFile(OpenImageEnDialog1.FileName);
stream.Position := 0;
MyQuery1.SQL.Text :=
'insert into `test` (pic_id, pic_name) values (null, :picname, :Image)';
MyQuery1.ParamByName('picname').AsString := OpenImageEnDialog1.FileName;
MyQuery1.ParamByName('Image').LoadFromStream(stream, ftBlob);
// MyQuery1.ParamByName('Image').LoadFromFile(OpenImageEnDialog1.FileName, ftBlob);
MyQuery1.Execute;
stream.Free;
end;
var
stream: TMemorystream;
begin
if not OpenImageEnDialog1.Execute then exit;
stream := TMemorystream.Create;
stream.LoadFromFile(OpenImageEnDialog1.FileName);
stream.Position := 0;
MyQuery1.SQL.Text :=
'insert into `test` (pic_id, pic_name) values (null, :picname, :Image)';
MyQuery1.ParamByName('picname').AsString := OpenImageEnDialog1.FileName;
MyQuery1.ParamByName('Image').LoadFromStream(stream, ftBlob);
// MyQuery1.ParamByName('Image').LoadFromFile(OpenImageEnDialog1.FileName, ftBlob);
MyQuery1.Execute;
stream.Free;
end;
retrieve data
Hi
I have used that data to save a Blob and the database shows that there is data because it now has 2.6Kb as Blob size
However when I try to retrieve the data and put it back in a TmemoryStream there is no data
BlobField := FieldByName(Field) AS TBlobField;
BlobField.SaveToStream(BlobStrm);
Should I be doing something different to put the data back into a Tmemory stream?
Thanks
I have used that data to save a Blob and the database shows that there is data because it now has 2.6Kb as Blob size
However when I try to retrieve the data and put it back in a TmemoryStream there is no data
BlobField := FieldByName(Field) AS TBlobField;
BlobField.SaveToStream(BlobStrm);
Should I be doing something different to put the data back into a Tmemory stream?
Thanks
{
CREATE TABLE `test` (
`Pic_Id` int(11) NOT NULL AUTO_INCREMENT,
`image` longblob,
`Pic_Name` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`Pic_Id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
}
procedure TForm1.btn1Click(Sender: TObject);
begin
myqry1.SQL.Text := 'insert into test (image, pic_name) values (:image, :pic_name)';
myqry1.ParamByName('image').LoadFromFile('c:\3.jpg', ftBlob);
myqry1.ParamByName('pic_name').AsString := '錦';
myqry1.ExecSQL;
end;
procedure TForm1.con1AfterConnect(Sender: TObject);
begin
con1.ExecSQL('set names utf8', [])
end;
Any one can execute ?
CREATE TABLE `test` (
`Pic_Id` int(11) NOT NULL AUTO_INCREMENT,
`image` longblob,
`Pic_Name` varchar(80) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`Pic_Id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
}
procedure TForm1.btn1Click(Sender: TObject);
begin
myqry1.SQL.Text := 'insert into test (image, pic_name) values (:image, :pic_name)';
myqry1.ParamByName('image').LoadFromFile('c:\3.jpg', ftBlob);
myqry1.ParamByName('pic_name').AsString := '錦';
myqry1.ExecSQL;
end;
procedure TForm1.con1AfterConnect(Sender: TObject);
begin
con1.ExecSQL('set names utf8', [])
end;
Any one can execute ?
-
AndreyZ
-
AndreyZ
Try using this code:
Code: Select all
con1.ExecSQL('set names utf8', []);
con1.ExecSQL('insert into batch set ImagePath=''C:\图像'', BatchName=''图像''', []);