问题
I'm working with a visual fox pro database (.dbf file) and I'm using the dbf python module. Heres an example:
myDb = VfpTable('table.dbf');
Now I can exclude deleted items with this by doing the following:
myDb._use_deleted = None;
My question(s) is/are is there an easier way to do this? Maybe a function? I hate accessing "private" variables. Also, without setting this property, how can I determine if a row has been deleted? They are still technically in the database so is there a flag? A hidden column? Maybe someone with more knowledge of this python module or Visual Fox Pro has some ideas.
回答1:
Stop me if you've heard this one before: """I have a DBF reading module (pydbfrw) which I've been meaning to release "one of these days"."""
It was easier to add the functionality that you want than to puzzle through the source code of the dbf
module:
>>> import pydbfrw
>>> d = pydbfrw.DBFreader('/devel/dbf/all_files/del.dbf')
>>> list(d)
[['fred', 1], ['harriet', 4]]
>>> d.get_field_names()
['NAME', 'AMT']
>>> d = pydbfrw.DBFreader('/devel/dbf/all_files/del.dbf', include_deleted=True)
>>> list(d)
[[False, 'fred', 1], [True, 'tom', 2], [True, 'dick', 3], [False, 'harriet', 4]]
>>> d.get_field_names()
['deleted__', 'NAME', 'AMT']
>>> for rowdict in d.get_dicts():
... print rowdict
...
{'deleted__': False, 'name': 'fred', 'amt': 1}
{'deleted__': True, 'name': 'tom', 'amt': 2}
{'deleted__': True, 'name': 'dick', 'amt': 3}
{'deleted__': False, 'name': 'harriet', 'amt': 4}
>>> for rowtup in d.get_namedtuples():
... print rowtup
...
Row(deleted__=False, name='fred', amt=1)
Row(deleted__=True, name='tom', amt=2)
Row(deleted__=True, name='dick', amt=3)
Row(deleted__=False, name='harriet', amt=4)
>>>
回答2:
Using the dbf module referenced above what you want is:
myDB.use_deleted = False
and for individual records:
for record in myDB:
if record.has_been_deleted:
print "record %d is marked for deletion!" % record.record_number
To physically remove the records from the table:
myDB.pack()
disclosure: I am the author.
来源:https://stackoverflow.com/questions/5424106/visual-fox-pro-and-python