DBFLibDBF and FPT files for Ring

Complete example

Create, fill, search, update, export, pack and close a table.

Complete Example#

Ring
load "dbflib.ring"

# -------------------------------------------------------
# 1. Create a table
# -------------------------------------------------------
oDbf = new DBFFile
oDbf.create("staff.dbf", [
    ["ID",      "N",  6, 0],
    ["NAME",    "C", 30, 0],
    ["DEPT",    "C", 20, 0],
    ["SALARY",  "N", 12, 2],
    ["ACTIVE",  "L",  1, 0],
    ["BIO",     "M", 10, 0]
])

# -------------------------------------------------------
# 2. Insert records — replace() works on all field types
# -------------------------------------------------------
aStaff = [
    [1, "Ahmed Al-Rashid", "Engineering", 85000.00, true,  "Lead engineer."],
    [2, "Fatima Hassan",   "HR",          72000.00, true,  "HR director."],
    [3, "Omar bin Said",   "Finance",     65000.00, false, "Contract ended."],
    [4, "Sara Abdullah",   "Engineering", 91000.00, true,  "VP Engineering."]
]

nCount = len(aStaff)
for i = 1 to nCount
    r = aStaff[i]
    oDbf.append()
    oDbf.replace("ID",     r[1])
    oDbf.replace("NAME",   r[2])
    oDbf.replace("DEPT",   r[3])
    oDbf.replace("SALARY", r[4])
    oDbf.replace("ACTIVE", r[5])
    oDbf.replace("BIO",    r[6])   # memo — no special function needed
next

# -------------------------------------------------------
# 3. Read all records — fieldGet() works on all field types
# -------------------------------------------------------
? "=== All Staff ==="
oDbf.goTop()
while ! oDbf.isEof()
    ? trim(oDbf.fieldGet("NAME")) + " (" + trim(oDbf.fieldGet("DEPT")) + ")"
    ? "  Bio: " + oDbf.fieldGet("BIO")   # memo — same as any other field
    oDbf.skip(1)
end

# -------------------------------------------------------
# 4. Search
# -------------------------------------------------------
? ""
? "=== Engineering Department ==="
if oDbf.locate("DEPT", "Engineering")
    ? trim(oDbf.fieldGet("NAME"))
    while oDbf.locateNext("DEPT", "Engineering")
        ? trim(oDbf.fieldGet("NAME"))
    end
ok

# -------------------------------------------------------
# 5. Update — replace() on a memo field works like any field
# -------------------------------------------------------
oDbf.goTo(1)
oDbf.replace("SALARY", 90000.00)
oDbf.replace("BIO",    "Lead engineer. Promoted 2024.")

# -------------------------------------------------------
# 6. Export — toList() includes memo content automatically
# -------------------------------------------------------
aAll = oDbf.toList()
? ""
? "Exported " + len(aAll) + " records"
? "Record 1 BIO: " + aAll[1][6]   # memo text in column 6

# -------------------------------------------------------
# 7. Delete inactive staff and pack (compacts DBF + FPT)
# -------------------------------------------------------
oDbf.goTop()
while ! oDbf.isEof()
    if ! oDbf.fieldGet("ACTIVE")
        oDbf.deleteRec()
    ok
    oDbf.skip(1)
end
oDbf.pack()
? "Records after pack: " + oDbf.recCount()

# -------------------------------------------------------
# 8. Set code page and show info
# -------------------------------------------------------
oDbf.setCodePage(C_CODEPAGE_WIN_1256)
? ""
? oDbf.info()

# -------------------------------------------------------
# 9. Copy structure to a new file
# -------------------------------------------------------
oDbf.copyStructure("staff_archive.dbf")

# -------------------------------------------------------
# 10. Close
# -------------------------------------------------------
oDbf.close()
DBFLib - DBF and FPT files for Ring, made with ringdoc.