Hi,
I would like to save a object that I created to database. I got this far looking at other solutions on EE (Save object in data base, Q_11075005).
I have created a Firebird database with two fields (Supplier_ID - Integer, sup_option - Binary BLOB).
The code below works not 100%. In Button1Click I write my object to database. The record is created and all looks good. But when I read the blob field from DB to populate my user object (Button2Click) none of the properties of the object are set.
One thing I have noticed is that 'sizeof(usr)' (Button1Click) always returns 4, no matter how long I make my Fullname and Password properties. Is this correct? I guess this is my problem. Only 4 bytes of my object are written to the DB.
procedure TForm_Main.Button1Click(Se
nder: TObject);
var
usr : TSPLoginUser;
bstream : TStream;
begin
//The object that I would like to store in the DB
usr := TSPLoginUser.Create;
usr.Username := 'addey';
usr.Fullname := 'Jacques';
usr.Password := 'password';
//Open query and create new record.
dm.IBOQuery1.Open;
dm.IBOQuery1.Append;
dm.IBOQuery1SUPPLIER_ID.Va
lue := 5;
bstream := dm.iboQuery1.CreateBlobStr
eam(dm.IBO
Query1SUP_
OPTION,bmW
rite);
bstream.Write(usr,sizeof(u
sr)); //SizeOf(usr) always returns 4
dm.IBOQuery1.Post;
dm.IBOQuery1.Close;
end;
procedure TForm_Main.Button2Click(Se
nder: TObject);
var
usr : TSPLoginUser;
stream : TStream;
begin
dm.IBOQuery1.Open;
stream := dm.IBOQuery1.CreateBlobStr
eam(dm.IBO
Query1SUP_
OPTION,bmR
ead);
stream.Read(usr,sizeof(usr
));//After
this none of the usr object's properties are set
dm.IBOQuery1.Close;
end;
Thanks!
Start Free Trial