4

好吧,我在这里使用一种古老的数据库格式,dbf 文件。不要问为什么,只知道某个软件决定扩展foxpro支持是因为微软决定扩展foxpro支持。现在,我在特定文件上收到以下错误。我已经成功加载了另一个文件,我很好奇这个数据库是否有问题。我相信您可能需要查看数据库来确定这一点,但它的发布方式非常庞大,所以我会尽我所能。

Traceback (most recent call last):
  File "billsapi.py", line 250, in <module>
    x.getUsedGuns()
  File "billsapi.py", line 72, in getUsedGuns
    itemdb = dbf.Dbf('item.dbf', readOnly=True, ignoreErrors=True)
  File "C:\Python27\lib\site-packages\dbfpy\dbf.py", line 135, in __init__
    self.header = self.HeaderClass.fromStream(self.stream)
  File "C:\Python27\lib\site-packages\dbfpy\header.py", line 127, in fromStream
    _fld = fields.lookupFor(_data[11]).fromString(_data, _pos)
  File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 455, in lookupFor
    return _fieldsRegistry[typeCode]
KeyError: '0'

这是我返回此错误的简单代码:

def getUsedGuns(self): 
    itemdb = dbf.Dbf('item.dbf', readOnly=True, ignoreErrors=True) 

就像我说的那样,我可以毫无问题地加载其他文件,但也许可以解决这个特定错误?

编辑:我还想指出,可以在 DBF View Plus 中打开、查看和修改该文件。

编辑:找到解决方案。我实际上最终使用了 python dBase 模块。我认为我的主要问题是没有备忘录文件(无论它们是什么,它都有一个 .fpt 文件扩展名)。这是我目前使用的:

from dbf.tables import VfpTable
itemdb = VfpTable('item.db')
for rec in itemdb:
    print rec['MY_COLUM_NAME']

我还想指出,目前仍在使用 FoxPro 的任何人都应该被烧毁。

4

3 回答 3

2

您的回溯是 dbfpy 告诉您文件具有不受 dbfpy 支持的字段类型代码的方式,0. 这是一个 Visual FoxPro(“VFP”)的东西。

This is nothing to do with memo files. Yes, if there are memo fields, they are stored in a .FPT file. foo.fpt needs to be present when you access foo.dbf.

You say """I actually ended up using the python dBase module""" ... presumably you mean Ethan Furman's dbf module, which according to its PyPI entry doesn't support null fields.

I have a DBF reading module (pydbfrw) which I've been meaning to release "one of these days". Here's an extract from its docs:

Field Type      Description  DBF variety  Python 2.x type
0 (digit zero)  _NullFlags   VFP          N/A             

Notes: This field type is used only for the hidden _NullFlags field which
is a bit mask saying which fields in the record should be interpreted as NULL.

My module implements recognising that and returning None for a field value where required. If you would like a copy of the module, find my e-mail address -- e.g. google("john machin xlrd") -- e-mail me and I'll send it to you.

于 2011-03-11T21:46:05.467 回答
0

可能是 item.dbf 使用了其他 dbf 没有的更高级的 dbf 功能。例如,自动递增整数是很晚才引入的,大多数 odbc 驱动程序都不支持。

于 2011-03-11T18:15:49.047 回答
0

You should take a look at Sybase's Advantage Database Server. This product has excellent support for VFP DBF files. I have been using their ODBC drivers with Python via pyodbc for a few years, and have had excellent results using their recently released a DB-API 2.0 compliant Python driver.

I too have been put in the position where I must support DBF tables. The Advantage Database Server has been an absolute lifesaver.

于 2011-03-14T09:54:44.887 回答