简体   繁体   English

Python DBF。 使用fpt备注字段将记录添加到dbf表

[英]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. 我试图将记录添加到具有索引文件和备忘录文件的现有和空DBF表(Fox Pro)。 It already exists in my folder: 它已存在于我的文件夹中:

Table.dbf
Table.fpt
Table.cdx

Table.dbf has three fields: Table.dbf有三个字段:

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 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 问题是当字段是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 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. 谢谢。

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) 将数据作为tuple传递(字段必须按顺序)

  • pass the data in as a dict 将数据作为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'} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM