I'm using the dbf library with python3.5.
The DBF table has a column with only dates without time and another just with time. Want to retrieve records from the last five minutes.
I'm new to this module and currently see just two approaches to get a portion of the data stored in a DBF:
First, with the sympathetic SQL like query:
records = table.query("SELECT * WHERE (SA03 BETWEEN " + beforedfilter + " AND " + nowdfilter + ") AND (SA04 BETWEEN " + beforetfilter + " AND " + nowtfilter + ")")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 3: ordinal not in range(128)
index = table.create_index(lambda rec: rec.SA03)
records = index.search(match=(?!))
The simplest way is to have a filter function that only tracks matching records:
# untested, but the idea is sound ;) def last_five_minutes(record, date_field, time_field): now = dbf.DateTime.now() if ( record[date_field] is not None and record[date_field] == now.date() and record[time_field] is not None and now.time()-record[time_field] <= datetime.timedelta(seconds=300) ): return record else: return dbf.DoNotIndex
and then use it:
index = table.create_index( lambda rec: last_five_minutes(rec, 'date_field', 'time_field'))