Application Development with Harbour/Database Connectivity

From Wikibooks, open books for an open world
Jump to: navigation, search

Create and fill a dbf file[edit]

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
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

Tip: Download libre office calc. With libre office calc you can open the testdbstructure.dbf and see content of the dbf file (http://www.libreoffice.org/)
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 libre office calc to check the Contacts.dbf