Search code examples
pythonvisual-foxprofoxprodbf

Python DBF. Add record to dbf table with fpt memo field


I am trying to add a record to an existing and empty DBF table (Fox Pro) that has an index file and a memo file. It already exists in my folder:

Table.dbf
Table.fpt
Table.cdx

Table.dbf has three fields:

Field1 (Integer)
Field2 (Character)
Field3 (Memo)

I use the method as described in

http://stackoverflow.com/questions/37891686/how-to-add-a-field-to-a-dbf-file

It is as follows:

import dbf

db = dbf.Table('table.dbf')
db.open() 
rec = dbf.create_template(db) 
rec.field1 = 9 
rec.field2 = ('some text')
db.append(rec)

So far so good. The problem is when a field is of type memo

Db = dbf.Table ('table.dbf')
Db.open ()
Rec = dbf.create_template (db)
Rec.field1 = 9
Rec.field2 = ('some text')
Rec.field3 = ('This is a long text')
Db.append (rec)

Then I have an error message:

Traceback (most recent call last):
   File "dbf12.py", line 8, in <module>
     Rec.field3= ('This is a long text')
   File "...libsite-packages\dbf\ver_33.py", line 2959, in __setattr__
     Self._dirty = True
   File "...libsite-packages\dbf\ver_33.py", line 2956, in __setattr__
     Raise FieldMissingError (name)
Dbf.ver_33.FieldMissingError: '_dirty: no such field in table'

I have looked at a similar question in

Http://stackoverflow.com/questions/16682470/using-python-to-write-dbf-table-with-fpt-memos?rq=1

I tried to change:

Db = dbf.Table ('table.dbf', dbf_type = 'Vfp')

But the result is the same.

Does anyone know the correct way to enter the memo field?

Thanks.


Solution

  • I'm not seeing this behavior in the latest version, but if you are unable to upgrade you have a couple other choices:

    • pass the data in as a tuple (fields must be in order)

    • pass the data in as a dict

    For example:

    db.append( (9, 'some text', 'a lot of text') )
    

    or

    db.append( {'field1': 9, 'field3': 'a lot of text', 'field2': 'some text'} )