用Python有效地查询DBF文件

2024-06-13 13:21:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从遗留的VFPDBF数据库中读取数据,并在本周内收集所有具有etd的行

我使用的是^{},但是在查询表时,它似乎从表中的第一条记录开始查询。这会导致在上周内试图查找数据时出现性能问题,因为它每次运行时都必须遍历数据库中的每一行(60k+)


table = dbf.Table(r'\\server\file.dbf')

table.open()

for row in table:

    if (self.monday < row.etd < self.friday) and ('LOC' not in row.route):
        self.datatable.Rows.Add(row.manifest, row.route, row.etd, row.eta, row.inst, row.subname)
    else:
        continue

我试图用for row in table[::-1]:来“反转”表格

但是,这与我认为在[::-1]之前将数据库加载到内存所需的时间相同

查询这些DBF文件的更有效方法是什么


Tags: 数据inself数据库for记录table读取数据
1条回答
网友
1楼 · 发布于 2024-06-13 13:21:23

正如您所知,dbf不支持索引文件。然而,它确实有一些让人想起VFP的方法,可以帮助:

# untested

table = ...

potental_records = []
with table:               # auto opens and closes
    table.bottom()        # goes to end of table
    while True:
        table.skip(-1)    # move to previous record
        row = table.current_record
        if self.monday > row.etd:
            # gone back beyond range
            break
        elif row.etd < self.friday:
            potential_records.append(row)

# at this point the table is closed and potential_records should have all
# records in the etd range.

只有当记录按etd进行物理排序时,上述操作才有效

相关问题 更多 >