pythonfoxprodbf

How to ignore deleted records using dbf-module in python?


I'm using dbf-module by Ethan Furman version 0.96.005 (latest one) in Python 2.7 using old-fashioned FoxPro2.x-tables. Since I want to ignore deleted records, I set tbl.use_deleted = False after assigning tbl = dbf.Table(dbf_path). I tried to set this before and after opening the table doing with tbl.open('read-only') as tbl: ..., but neither this nor that seems to have any effect.

On record-level I tried:

for rec in tbl:
    if not rec.has_been_deleted and ...

but that gave me:

FieldMissingError: 'has_been_deleted:  no such field in table'

Am I doing s.th. wrong? Or is that feature not available any more (as it was 5 years ago - see Visual Fox Pro and Python)?


Solution

  • use_deleted and has_been_deleted no longer exist, and have been replaced with the function is_deleted.

    So your choices at this point are (assuming a from dbf import is_deleted):

    # check each record
    for rec in tbl:
        if is_deleted(rec):
            continue
    

    or

    # create active/inactive indices
    
    def active(rec):
        if is_deleted(rec):
            return DoNotIndex
        return dbf.recno(rec)
    
    def inactive(rec):
        if is_deleted(rec):
            return recno(rec)
        return DoNotIndex
    
    active_records = tbl.create_index(active)
    
    deleted_records = tbl.create_index(inactive)
    

    and then iterate through those:

    # check active records
    for rec in active_records:
        ...