| Cool Things: Read dBASE Files Easily! |
|
|
|
| Tips & Techniques - RPG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Written by Mike Faust | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Friday, 20 April 2012 00:00 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This simple service program makes dBASE file access simple. Written by Mike Faust dBASE has been around since the late 1970s/early 1980s. It was the first widely used relational database for PCs. I recently had a need to import data into the System i from the U.S. Census Bureau's Tiger database, which stores address data for all of the United States, including state, county, and city. The Tiger database is made up of multiple dBASE format files, so I needed to create a means by which I could read the data directly from the System i. In this tip, we'll take a look at a simple service program that gives you the ability to do sequential data reads from a dBASE file. dBASE Table File FormatThe dBASE file format is actually relatively easy to read. Tables contain header information that describes the table itself, along with a field descriptor array that defines all of the fields in the file. The data immediately follows the header information and is broken apart based on the field descriptors. Information on the dBASE file layout can be found on the dBASE Web site.
The field descriptor array contains one element for each field in the table. These values describe the field names and the type of data contained in the field. You can determine the end of the field descriptor array by the line field character (hex 0D).
Since all of this information is stored sequentially within the file, it's easy enough to create a service program that reads the file from the IFS and returns the information that is stored in the table file. The Service ProgramTo allow for easy access to dBASE table files, I've created a simple service program. The source for this service program can be found here. The source for the service program contains three members.
The process of compiling the service program involves first creating the RPGLE module using the CRTRPGMOD command:
CRTRPGMOD MODULE(MYLIB/DBASEF)
Next, the service program is created using the CRTSRVPGM command:
CRTSRVPGM SRVPGM(DBASEF) EXPORT(*SRCFILE) SRCFILE(MYLIB/QRPGLESRC) SRCMBR(DBASEFBND)
This service program contains five sub-procedures:
These subprocedures use a set of data structures to store data associated with the file header and field data. Below are descriptions of these data structures.
The data structure in Table 1 is generated by the opendBASEFile subprocedure. Within that data structure, multiple copies of the field description data structure shown in Table 2 are generated. That structure contains all of the information for each field in the table. The data structure described in Table 3 shows the data returned by the getdBASEField and getdBASEFieldByName subprocedures. Sample ProgramTo see how to use the dBASEf service program, take a look at the TESTDBF RPGLE program shown below:
//******************************************************************** // Purpose: Test Program //********************************************************************
h dftactgrp(*no) h option(*nodebugio:*srcstmt:*seclvl) h bnddir('TESTDIR')
/copy qrpglesrc,DBASEFPRC dtestdbf pr d prmdBASEfile 500a dtestdbf pi d prmdBASEfile 500a
d wkDSHeader ds likeDs(dbfHeader) based(ptrHeader) d wkRecordStr s 65535a based(ptrRecord) d wkRecordCount s 5 0 d wkIndex s 3 0 d wkField ds likeds(fieldData) d wkStr s 32766a
/free
ptrHeader = openDBaseFile(prmDBaseFile);
wkRecordCount = 0;
ptrRecord = readDBASEFile(ptrHeader); dow ptrRecord <> *null and wkRecordCount < wkDSHeader.RecordCount; wkRecordCount += 1; wkField = getDBaseField(ptrHeader: ptrRecord: 1); wkStr = wkField.String; dsply %subst(wkStr: 1: 50); ptrRecord = readDBASEFile(ptrHeader); enddo;
closeDBaseFile(ptrHeader); *INLR = *on; return;
/end-free
This sample program accepts a single parameter that contains the path to the dBASE file to be opened. It also assumes that the DBASEF service program has already been added to a binding directory named TESTDIR. This program simply opens the specified dBASE file using the opendBASEFile subprocedure, reads each record, and then displays the contents of the first field in that record using the getdBASEField subprocedure. Note that we could just as easily use the getdBASEFieldByName subprocedure, using a field name to retrieve the same information. Finally, the program closes the dBASE file using the closedBASEFile subprocedure. What It Won't DoThis simple service program does give powerful functionality, but what it won't allow you to do is update a dBASE file, nor will it allow you to read by keyed field or indexes. That being said, this service program does give you an easy way to access dBASE files inside of an ILE RPG program. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Last Updated on Friday, 20 April 2012 08:07 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||









