0

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.

nagylzs
  • 3,954
  • 6
  • 39
  • 70

2 Answers2

3

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

What you want is:

dbf.delete(rec)
tbl.pack()
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • Okay, and what about record.write_record() - do I need to write out changes somehow? – nagylzs Feb 16 '18 at 18:14
  • @nagylzs: Changes are written as you go. So if you don't want to save things one field at a time you have to store them up. Feel free to ask that as a question so I can write a better answer than this comment. :) – Ethan Furman Feb 17 '18 at 04:30
  • Asked here: https://stackoverflow.com/questions/48839421/how-to-modify-and-save-records-in-a-dbf-file-using-python-dbf-module – nagylzs Feb 17 '18 at 08:30
0

Alternatively you can use dbfpy module which allows you to access records public variable deleted. Example code:

from dbfpy import dbf
...
dbfFile = dbf.Dbf("DbfFileName", readOnly=True)
for rec in dbfFile:
  if rec.deleted:
    #action if deleted
dbfFile.close()
Lemjur
  • 338
  • 3
  • 11