使用 Harbour 進行應用程式開發/資料庫連線
步驟 1:建立一個名為 DbTests.prg 的檔案
步驟 2:在 DbTests.prg 檔案中寫入以下程式碼
PROCEDURE Main()
? "DbTests"
CREATE testdbstructure
RETURN
步驟 3:編譯並執行 DbTests.prg
這段 Clipper 程式碼在螢幕上顯示一條訊息,並建立一個名為 testdbstructure 的新資料庫結構檔案 - 您應該會看到在與 DbTests.prg 相同的目錄中建立了一個 testdbstructure.dbf 檔案,這個 DBF 檔案是描述其他 dbf 檔案的列名的行。
testdbstructure 的列是 FIELD_NAME、FIELD_TYPE、FIELD_LEN、FIELD_DEC。
步驟 4:建立 3 行,其中包含 FIELD_NAME、FIELD_TYPE、FIELD_LEN、FIELD_DEC,然後關閉當前 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
RETURN
這段程式碼使用 APPEND BLANK 命令向表中新增三個新的空白記錄。
這段程式碼然後使用 "->" 運算子訪問表中的欄位並定義欄位值。例如,命令 "FIELD->FIELD_NAME := "ContactId"" 將第一條記錄的 "FIELD_NAME" 欄位的值設定為 "ContactId"。另外兩個命令定義了剩餘記錄中 "Name" 和 "Email" 欄位的值。
最後,"Main" 過程以 "CLOSE" 命令結束,該命令關閉表,然後以 "RETURN" 命令結束,該命令將控制權返回給呼叫它的主程式或過程。
提示:要檢視 DBF 檔案的內容,可以使用 GNumeric (http://www.gnumeric.org/) 或 LibreOffice Calc (http://www.libreoffice.org/) - 大多數電子表格程式支援 DBF 檔案。
步驟 5:根據 testdbstructure.dbf 建立一個名為 contacts.dbf 的 dbf 檔案。現在 contacts.dbf 的列是 testdbstructure.dbf 的 FIELD_NAME 行。然後在 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
使用 LibreOffice Calc 開啟並檢視 Contacts.dbf 檔案的內容。
https://harbourlanguage.blogspot.com/2010/06/understanding-harbour-rdd.html