Solved

C++ Build: read data from BLOB?

Posted on 2001-08-01
9
812 Views
Last Modified: 2012-05-04
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(AnsiString 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*)Field;
TBlobStream* ThisBlobStream;
ThisBlobStream=new TBlobStream(ThisBlob, bmRead);

int PointsToRead=ThisBlobStream->Size/sizeof(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->Next();DBTable->Next();DBTable->Next();
DBTable->Next();DBTable->Next();  
}    //***
delete File;    
DBTable->RecNo = SaveNo;

}
0
Comment
Question by:Johncili
  • 6
  • 3
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6343734
guessing you misunderstand the next-method
(this goes to the next record and not to the next field)

therefore
DBTable->First();DBTable->Next();  
should be
DBTable->First();  //except you will skip the first record

and

DBTable->Next();DBTable->Next();DBTable->Next();DBTable->Next();
DBTable->Next();DBTable->Next();
should be
DBTable->Next(); //only one is needed to go to the next record

just a guess

meikl ;-)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6343745
also you could use the stream copy-method instead of saving bytewise
0
 

Author Comment

by:Johncili
ID: 6344711
Thanks,

However, how to write out those marked BLOB (every raw, second column, e.g., every record, 2nd field only)?

Looking for your advise.



0
 

Author Comment

by:Johncili
ID: 6344793

How to write out data in every 2nd field?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Johncili
ID: 6344868


If I know that the name of 2nd field is "BLOB2", how to write out only this field of every record?

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6344907
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(AFileName, 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[AFieldNo-1]).IsEmpty Then  //only for not empty contents
      begin
        TBlobField(ATable.Fields[AFieldNo-1]).SaveToStream(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 ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 299 total points
ID: 6344963
missed just the next :-))

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;
 if ATable.Fields[AFieldNo-1].IsBlob Then  //is it a blobfield
 begin
   fs := tfilestream.create(AFileName, fmCreate);  //new file
   ATable.First;  //from beginning
   while not ATable.Eof do //to end
   begin
     If Not TBlobField(ATable.Fields[AFieldNo-1]).IsEmpty Then  //only for not empty contents
     begin
       TBlobField(ATable.Fields[AFieldNo-1]).SaveToStream(fs);  //write to the stream, with a typeconversion
       fs.write(#13#10,2);  //new line
     end;
     ATable.Next; //next record
   end;
 end;
 fs.free;  //close file
end;

well, just from head, there may typos

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6351779
any success?
any problems?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6359877
john, did you get it work?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now