Johncili
asked on
C++ Build: read data from BLOB?
A table will form as below after running the C++ Builder code:
1 [BLOB] [*BLOB*] [BLOB] [BLOB] [BLOB] [BLOB] [Blob]
2 [BLOB] [*BLOB*] [BLOB] [BLOB] [BLOB] [BLOB] [Blob]
3 ......
1000[BLOB] [*BLOB*] [BLOB] [BLOB] [BLOB] [BLOB] [Blob]
I need that, after only click the second [BLOB], all data in this column of all raws will be saved in
ascii format. Howevr, only the data in the first BLOB was saved, all other data was NOT saved in the
file. How to modify it?
High points will be given if your modification does work!
//_________________ part of code _________________
void TMainForm::ExportBLOB(Ansi String FileName, AnsiString FieldName)
{
int SaveNo = DBTable->RecNo;
DBTable->First();DBTable-> Next();
TFileStream* File=new TFileStream(FileName, fmCreate);
while(!DBTable->Eof){ //***
TField* Field=DBTable->FieldByName (FieldName );
TFieldType FieldType=Field->DataType;
if(FieldType!=ftBlob)
throw Exception("Invalid Call To ExportBlob() - Field Not A BlobType");
TBlobField* ThisBlob;
ThisBlob=(TBlobField*)Fiel d;
TBlobStream* ThisBlobStream;
ThisBlobStream=new TBlobStream(ThisBlob, bmRead);
int PointsToRead=ThisBlobStrea m->Size/si zeof(short );
if(PointsToRead<=0)
{
delete ThisBlobStream;
return;
}
short* Data=new short[PointsToRead];
ThisBlobStream->Seek(0, soFromBeginning);
ThisBlobStream->Read(Data, ThisBlobStream->Size);
for(int i=0; i<PointsToRead; i++)
{
AnsiString Text;
Text+=Data[i];
Text+="\n";
File->Write(Text.c_str(), Text.Length());
}
delete ThisBlobStream;
delete [] Data;
DBTable->Next();DBTable->N ext();DBTa ble->Next( );DBTable- >Next();
DBTable->Next();DBTable->N ext();
} //***
delete File;
DBTable->RecNo = SaveNo;
}
1 [BLOB] [*BLOB*] [BLOB] [BLOB] [BLOB] [BLOB] [Blob]
2 [BLOB] [*BLOB*] [BLOB] [BLOB] [BLOB] [BLOB] [Blob]
3 ......
1000[BLOB] [*BLOB*] [BLOB] [BLOB] [BLOB] [BLOB] [Blob]
I need that, after only click the second [BLOB], all data in this column of all raws will be saved in
ascii format. Howevr, only the data in the first BLOB was saved, all other data was NOT saved in the
file. How to modify it?
High points will be given if your modification does work!
//_________________ part of code _________________
void TMainForm::ExportBLOB(Ansi
{
int SaveNo = DBTable->RecNo;
DBTable->First();DBTable->
TFileStream* File=new TFileStream(FileName, fmCreate);
while(!DBTable->Eof){ //***
TField* Field=DBTable->FieldByName
TFieldType FieldType=Field->DataType;
if(FieldType!=ftBlob)
throw Exception("Invalid Call To ExportBlob() - Field Not A BlobType");
TBlobField* ThisBlob;
ThisBlob=(TBlobField*)Fiel
TBlobStream* ThisBlobStream;
ThisBlobStream=new TBlobStream(ThisBlob, bmRead);
int PointsToRead=ThisBlobStrea
if(PointsToRead<=0)
{
delete ThisBlobStream;
return;
}
short* Data=new short[PointsToRead];
ThisBlobStream->Seek(0, soFromBeginning);
ThisBlobStream->Read(Data,
for(int i=0; i<PointsToRead; i++)
{
AnsiString Text;
Text+=Data[i];
Text+="\n";
File->Write(Text.c_str(), Text.Length());
}
delete ThisBlobStream;
delete [] Data;
DBTable->Next();DBTable->N
DBTable->Next();DBTable->N
} //***
delete File;
DBTable->RecNo = SaveNo;
}
also you could use the stream copy-method instead of saving bytewise
ASKER
Thanks,
However, how to write out those marked BLOB (every raw, second column, e.g., every record, 2nd field only)?
Looking for your advise.
However, how to write out those marked BLOB (every raw, second column, e.g., every record, 2nd field only)?
Looking for your advise.
ASKER
How to write out data in every 2nd field?
ASKER
If I know that the name of 2nd field is "BLOB2", how to write out only this field of every record?
well,
i can show only how i would do it in delphi
(because i'm no c-coder)
i guess your table contains 7 blob-fields
so i would code something like this
procedure blobexport(ATable : TTable; AFieldNo : Integer; AFilename : String);
var
fs : tfilestream;
begin
if not ATable.Active then //be sure the table is open
ATable.Open;
fs := tfilestream.create(AFileNa me, fmCreate); //new file
ATable.First; //from beginning
if ATable.Fields[AFieldNo-1]. IsBlob Then //is it a blobfield
begin
while not ATable.Eof do //to end
begin
If Not TBlobField(ATable.Fields[A FieldNo-1] ).IsEmpty Then //only for not empty contents
begin
TBlobField(ATable.Fields[A FieldNo-1] ).SaveToSt ream(fs); //write to the stream, with a typeconversion
fs.write(#13#10,2); //new line
end;
end;
end;
fs.free; //close file
end;
well, just from head, there may typos
meikl ;-)
i can show only how i would do it in delphi
(because i'm no c-coder)
i guess your table contains 7 blob-fields
so i would code something like this
procedure blobexport(ATable : TTable; AFieldNo : Integer; AFilename : String);
var
fs : tfilestream;
begin
if not ATable.Active then //be sure the table is open
ATable.Open;
fs := tfilestream.create(AFileNa
ATable.First; //from beginning
if ATable.Fields[AFieldNo-1].
begin
while not ATable.Eof do //to end
begin
If Not TBlobField(ATable.Fields[A
begin
TBlobField(ATable.Fields[A
fs.write(#13#10,2); //new line
end;
end;
end;
fs.free; //close file
end;
well, just from head, there may typos
meikl ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
any success?
any problems?
any problems?
john, did you get it work?
(this goes to the next record and not to the next field)
therefore
DBTable->First();DBTable->
should be
DBTable->First(); //except you will skip the first record
and
DBTable->Next();DBTable->N
DBTable->Next();DBTable->N
should be
DBTable->Next(); //only one is needed to go to the next record
just a guess
meikl ;-)