Application Development with Harbour/Database Connectivity
How to Create and Fill a DBF File
[edit | edit source]Step 1: Create a file DbTests.prg
Step 2: Write the following code in that file DbTests.prg:
PROCEDURE Main()
? "DbTests"
CREATE testdbstructure
RETURN
Step 3: Compile and run DbTests.prg
This code in Clipper displays a message on the screen and creates a new database structure file named testdbstructure - you should see that a testdbstructure.dbf is created in the same directory as DbTests.prg, this DBF file is a where columns names of other dbffiles are described in rows
The columns of this testdbstructure are FIELD_NAME, FIELD_TYPE, FIELD_LEN, FIELD_DEC.
Step 4: Create 3 rows which has a FIELD_NAME, FIELD_TYPE, FIELD_LEN, FIELD_DEC and then close the current dbf file
PROCEDURE Main()
? "DbTests"
CREATE testdbstructure
APPEND BLANK
FIELD->FIELD_NAME := "ContactId"
FIELD->FIELD_TYPE := "N"
FIELD->FIELD_LEN := 2
FIELD->FIELD_DEC := 0
APPEND BLANK
FIELD->FIELD_NAME := "Name"
FIELD->FIELD_TYPE := "C"
FIELD->FIELD_LEN := 20
FIELD->FIELD_DEC := 0
APPEND BLANK
FIELD->FIELD_NAME := "Email"
FIELD->FIELD_TYPE := "C"
FIELD->FIELD_LEN := 20
FIELD->FIELD_DEC := 0
CLOSE
RETURN
The code uses the APPEND BLANK command to add three new blank records to the table.
The code then uses the "->" operator to access the fields in the table and define the field values. For example, the command "FIELD->FIELD_NAME := "ContactId"" sets the value of the "FIELD_NAME" field in the first record to "ContactId". The other two commands define the values of the "Name" and "Email" fields in the remaining records.
Finally, the "Main" procedure ends with the "CLOSE" command, which closes the table, and the "RETURN" command, which returns control to the main program or procedure that called it.
Tip: to view the contents of a DBF file you can use GNumeric (http://www.gnumeric.org/) or LibreOffice Calc (http://www.libreoffice.org/) - most spreadsheet programs support DBF files.
Step 5: Create a dbf file with the name contacts.dbf from the testdbstructure.dbf. The columns of contacs.dbf are now the FIELD_NAME rows of testdbstructure.dbf. And then put in some testdata in Contacts.dbf
PROCEDURE Main()
? "DbTests"
CREATE testdbstructure
APPEND BLANK
FIELD->FIELD_NAME := "ContactId"
FIELD->FIELD_TYPE := "N"
FIELD->FIELD_LEN := 2
FIELD->FIELD_DEC := 0
APPEND BLANK
FIELD->FIELD_NAME := "Name"
FIELD->FIELD_TYPE := "C"
FIELD->FIELD_LEN := 20
FIELD->FIELD_DEC := 0
APPEND BLANK
FIELD->FIELD_NAME := "Email"
FIELD->FIELD_TYPE := "C"
FIELD->FIELD_LEN := 20
FIELD->FIELD_DEC := 0
CLOSE
CREATE Contacts FROM testdbstructure
APPEND BLANK
REPLACE ContactId WITH 1
REPLACE Name WITH "Peter Smith"
REPLACE Email WITH "peter123@email.nl"
APPEND BLANK
REPLACE ContactId WITH 2
REPLACE Name WITH "Michael Jones"
REPLACE Email WITH "michael123@email.nl"
RETURN
Use LibreOffice Calc to open and see the content of the file Contacts.dbf.
RDDs and Database Connectivity
[edit | edit source]https://harbourlanguage.blogspot.com/2010/06/understanding-harbour-rdd.html