Database Function Summary
Custom Database Applications
Despite having no built-in, standard database support, several of the file
and string functions can be used to create simple, fast database
applications where the database consists of records/lines of string data.
For example, suppose a database record includes a name, state and zip code and that a file "data.txt" contains the following three records.
Bob:::Texas:::75300
Mary:::Arkansas:::77001
Helen:::Maine:::45622
Note that any separating character string can be used as long as it will not be used as data in any of the records. In this example, ":::" was used because because no Name/State/Zip will never be that value.
A simple database application using this basic file structure would be as follows. The basic technique is to put each text line into a string array, then use PowerBASIC functions to pull out fields from each record as needed. When all changes are made to the array, simply write it back out to the file, overwriting any prior content.
1. Open Database (read text lines into array):
Open "data.txt" For Input as #1
Dim MyData() as String
'first way to create array with 1 record per element
'resizes array each time it reads a line of text
While NOT eof(1)
Line Input #1, MyData(Ubound)
if NOT eof(1) then
REDIM Preserve MyData(Ubound(MyData)+1)
end if
Wend
'second way to crate array with 1 record per element
'uses filescan to count number of lines
FILESCAN #1, RECORDS to iCount
REDIM MyDATA(iCount-1)
For i = 0 to UBound(MyData)
Line Input #1, MyData(i)
Next i
'third way to create array with 1 record per element
'if all records contain same number of characters
iCount = LOF(1) / iRecordSize
REDIM MyDATA(iCount-1)
For i = 0 to UBound(MyData)
Line Input #1, MyData(i)
Next i
Close #1
2. Get/Set Name/State/Zip from any array element
Dim RecordData(2) As String '3 elements
Dim iRecord as Long
For i = 0 to Ubound(MyArray)
'first way to get Name/State/Zip
PARSE MyData(iRecord), RecordData 'separates data
Name$ = MyData(0) 'optional, for convenience only
State$ = MyData(1) 'optional, for convenience only
Zip$ = MyData(2) 'optional, for convenience only
'second way to get Name/State/Zip
Name$ = Parse$(MyData(iRecord), ":::", 1)
State$ = Parse$(MyData(iRecord), ":::", 2)
Zip$ = Parse$(MyData(iRecord), ":::", 3)
'now use Name/State/Zip (including setting their values)
.... program statements using values
.... assign values to Name$, State$, Zip$
'save results
MyData(iRecord) = Build$(Name$, ":::", State$, ":::", Zip$)
Next i
2A. 'Use ARRAY INSERT to add records
'Use ARRAY DELETE to delete records
'Use ARRAY SCAN to search
'Use ARRAY SORT to sort records
3. Write changed array to file:
Open "data.txt" for Output as #1
For i = 0 to Ubound(MyArray)
Print #1, MyArray(i)
Next i
For syntax and examples, follow the links to FILESCAN, PARSE and REDIM. These functions were used in the above approaches to treating a text file as a database.
If you have any suggestions or corrections, please let me know.