2014-06-10 53 views
2

我試過兩個模塊,用於讀取dbf文件(dbf和dbfpy),但我必須通過記錄讀取數據庫記錄來查找內容。這對於大型數據庫來說確實很慢。是否有任何模塊可以處理查詢表或使用CDX索引?用python高效讀取foxpro DBF

+0

根據您的平臺,我想你可以嘗試操作系統級別的外部接口,如ODBC/JDBC。 –

回答

2

我不相信dbfpy支持索引文件,而且我知道dbf沒有。

然而,在dbf您可以創建一個臨時索引,然後查詢是:

big_table = dbf.Table('/path/to/some/big_table') 
def criteria(record): 
    "index the table using these fields" 
    return record.income, record.age 
index = big_table.create_index(key=criteria) 

index現在可以itereated過,或搜索返回所有匹配的記錄:

for record in index.search(match=(50000, 30)): 
    print record 

樣本表:

table = dbf.Table('tempu', 'name C(25); age N(3,0); income N(7,0);') 
table.open() 
for name, age, income in (
     ('Daniel', 33, 55000), 
     ('Mike', 59, 125000), 
     ('Sally', 33, 77000), 
     ('Cathy', 41, 50000), 
     ('Bob', 19, 22000), 
     ('Lisa', 19, 25000), 
     ('Nancy', 27, 50000), 
     ('Oscar', 41, 50000), 
     ('Peter', 41, 62000), 
     ('Tanya', 33, 125000), 
     ): 
    table.append((name, age, income)) 

index = table.create_index(lambda rec: (rec.age, rec.income)) 

還有碰到部門首長搜索範圍的開頭和結尾:

# all the incomes of those who are 33 
for rec in index.search(match=(33,), partial=True): 
    print repr(rec) 
print 
# all the incomes of those between the ages of 40 - 59, inclusive 
start = index.index_search(match=(40,), nearest=True) 
end = index.index_search(match=(60,), nearest=True) 
for rec in index[start:end]: 
    print repr(rec) 

它打印:

Daniel     33 55000 
Sally      33 77000 
Tanya      33 125000 

Cathy      41 50000 
Oscar      41 50000 
Peter      41 62000 
Mike      59 125000 
+0

這花了大約兩倍的時間。我猜,因爲它必須創建索引,然後通過它循環。 – user3727436

+0

@ user3727436:你能編輯你的答案併發布你試過的代碼嗎?創建索引會循環遍歷整個表,但在此之後(使用適當的成語)查找應該使用二進制algorythm。 –