指针、Stream、BLOB 问题 (50分)

  • 主题发起人 主题发起人 leyon328
  • 开始时间 开始时间
L

leyon328

Unregistered / Unconfirmed
GUEST, unregistred user!
数据库表中有BLOB字段,保存一个不定长的smallint数组,已经有好多记录了,现在要
从表中取出此字段的值,并一一显示数组中的值。

procedure TForm1.DBGrid1DblClick(Sender: TObject);
Var
tmpStream : TMemoryStream;
tmpField : TBlobField;
dst_f_hdl : Integer;
kk,len : smallint;
buf : psmallint;
begin

len := ADOQuery1.FieldByName('len').asinteger;

tmpStream := TMemoryStream.Create;
tmpStream.Position := 0;
tmpField := (TBlobField(ADOQuery1.FieldByName('myblob')));
tmpField.SaveToStream(tmpStream);

getmem(buf,len*sizeof(smallint));
tmpStream.Position := 0;
len := tmpStream.Read(buf,len*sizeof(smallint));
tmpStream.Free;


// kk := smallint(buf^);//此处如何一一取出数组中的值。

freemem(buf);

//临时看看保存为文件咋样,结果是对的。
dst_f_hdl := FileCreate('1.p');
FileSeek(dst_f_hdl,0,0);
FileWrite(dst_f_hdl,buf,len*sizeof(smallint));
FileClose(dst_f_hdl);
end;
 
var
kkk : integer;
pint : pinteger;
p1:Pinteger;
begin
getmem(pint,100);
//从文件中读出数据
P1:=pint;
Inc(P1,4);
kkk:=P1^;
ShowMessage(IntToStr(KKK));
//kkk := pint[3]
//如何得到第四个integer的值???????????
freemem(pint);
end;
 
P1:=pint;
inc(p1,4*3);
kkk:=p1^;
 
大家要是能试试就好了
 
//读取array
procedure TFrmTest.LoadArray(var ary: TPointArray
Field: TBlobField);
var
aStream : TMemoryStream;
aSingle : TPoint;
i : Integer;
begin
aStream := TMemoryStream.Create;
try
Field.SaveToStream(aStream);
SetLength(ary, aStream.Size div SizeOf(aSingle));
aStream.Position :=0;
for i := 0 to Length(ary) - 1 do
begin
aStream.ReadBuffer(aSingle, SizeOf(aSingle));
ary := aSingle;
end;
finally
aStream.Free;
end;
end;
//保存array
procedure TFrmTest.SaveArray(ary: TPointArray
Field: TBlobField);
var
aStream : TMemoryStream;
aSingle : TPoint;
i,nSize : Integer;
begin
aStream := TMemoryStream.Create;
try
nSize := aStream.Size;
for i := 0 to Length(ary) - 1 do
begin
aSingle := ary;
aStream.WriteBuffer(aSingle, SizeOf(aSingle));
end;
aStream.Position :=0;
Field.LoadFromStream(aStream);
finally
aStream.Free;
end;
end;
 
接受答案了.
 
后退
顶部