Search code examples
pythonvisual-foxprodbf

How to delete record from dbf file using python dbf module?


I'm trying to write/delete records in a visual foxpro 6 dbf file, using python 2.7 and the dbf package:

import dbf
tbl = dbf.Table('test.dbf')
tbl.open()
rec = tbl[0]
print(rec.__class__)
rec.delete_record()

Result:

<class 'dbf.ver_2.Record'>
Traceback (most recent call last):
  File "C:/Python/Projects/test/test.py", line 11, in <module>
    rec.delete_record()
  File "C:\Python\Projects\test\venv\lib\site-packages\dbf\ver_2.py", line 2503, in __getattr__
    raise FieldMissingError(name)
dbf.ver_2.FieldMissingError: 'delete_record:  no such field in table'

Here is the documentation for that package: http://pythonhosted.org/dbf/

The record object really does not have this method, but it is documented. The table is opened in read-write mode. (But it is also true that the Table() constructor should return an opened table, but it returns a closed table instead.)

What am I doing wrong?

The biggest problem is that there are no other options. The only other package I know of is "dbfpy" but that does not handle vfoxpro 6 tables, and it does not handle different character encodings.


Solution

  • That documentation is out of date. (My apologies.)

    What you want is:

    dbf.delete(rec)
    tbl.pack()