Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-03-2006, 06:30 PM
lspinks lspinks is offline
Senior Member
 
Join Date: Nov 2006
Posts: 244
Default Save and Load Table Data to and from a file

I need to save my table data to a file
and be able to read it back into a table

The "save to file" seems to be working, but you may want to look it over - cause it may need help in getting the Load from file to work

Save To File:

procedure TForm1.Button1Click(Sender: TObject);
var
FS: TFileStream;
BS: TADOBlobStream;
begin
if SaveDialog1.Execute then
begin
FS:= TFileStream.Create(SaveDialog1.FileName, fmCreate);
Table1.DisableControls;
Table1.First;
try
WriteStreamInt(FS, Table1.RecordCount);
while not Table1.EOF do
begin
WriteStreamStr(FS, Table1.FieldByName('Title').asString);
WriteStreamInt(FS, Table1.FieldByName('Year').asInteger);
WriteStreamStr(FS, Table1.FieldByName('Finish').asString);
if not Table1Icon.IsNull then
begin
WriteStreamBool(FS, True);
BS:= TADOBlobStream.Create(Table1Icon, bmRead);
BS.SaveToStream(FS);
BS.Free;
end
else
WriteStreamBool(FS, False);

if not Table1Image.IsNull then
begin
WriteStreamBool(FS, True);
BS := TADOBlobStream.Create(Table1Image, bmRead);
BS.SaveToStream(FS);
bs.Free;
end
else
WriteStreamBool(FS, False);
Table1.Next;
end;
finally
FS.Free;
end;
Table1.EnableControls;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
FS: TFileStream;
BS: TADOBlobStream;
I, CCount, Yr: Integer;
TitleStr, FinishStr: String;
JpgIcon, JpgImage: TJPegImage;
hasIcon, hasImage: Boolean;
begin
if OpenDialog1.Execute then
begin
FS:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
try
CCount:= ReadStreamInt(FS);
for I:= 1 to CCount do
begin
TitleStr:= ReadStreamStr(FS);
Yr:= ReadStreamInt(FS);
FinishStr:= ReadStreamStr(FS);
hasIcon:= ReadStreamBool(FS);
if hasIcon = True then
begin
jpgIcon := TJpegImage.Create;
//Stream.Position := 0;
jpgIcon.LoadFromStream(FS);
end;

if hasImage = True then
begin
jpgIcon := TJpegImage.Create;
//Stream.Position := 0;
jpgIcon.LoadFromStream(FS);
end;

Table1.Insert;
Table1.FieldByName('Title').asString:= TitleStr;
Table1.FieldByName('Year').asInteger:= Yr;
Table1.FieldByName('Finish').asString:= FinishStr;

if Assigned(JpgIcon) then
begin
bs := TADOBlobStream.Create(Table1Icon, bmWrite);
JPGIcon.SaveToStream(bs);
bs.Free
end;

if Assigned(JpgImage) then
begin
bs := TADOBlobStream.Create(Table1Image, bmWrite);
JPGImage.SaveToStream(bs);
bs.Free
end;
Table1.Post;
end;
finally
JpgIcon.Free;
JpgImage.Free;
FS.Free;
end;
end;
end;

thanks

leon
Reply With Quote
  #2  
Old 12-03-2006, 11:55 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,335
Default RE: Save and Load Table Data to and from a file

And what part of the loading fails ??? Do you get Access Violation, is your result empty, what ???

And what did your debugger say when reading the data back ??? At which point does it contain the invalid data ???

Objective reality is a delirium caused by lack of alcohol in blood.
There is no place like 127.0.0.1
Reply With Quote
  #3  
Old 12-04-2006, 03:49 AM
lspinks lspinks is offline
Senior Member
 
Join Date: Nov 2006
Posts: 244
Default RE: Save and Load Table Data to and from a file

The Load from file is where I crash:


if Assigned(JpgImage) then
begin
bs := TADOBlobStream.Create(Table1Image, bmWrite);
JPGImage.SaveToStream(bs); //**** CRASHES HERE
bs.Free
end;



I get an access violation (EAccessViolation) at the point
marked //***** CRASHES HERE


The Save to file code i posted does not crash, but Im not sure if its saving the data to the file correctly (hense the load from file crash)


I have a table with the following fields:

Title : String
Year : Integer
Finish : String

Icon : Blob (Jpg)
Image : Blob (Jpg)


Im trying to step through table and save the records to a file.

Then I want to step through the file and load them into a table


thanks

leon
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:26 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.